473,327 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

how to count spaces in a file in python?

I want to write a function that takes a file and a character and returns the number of character, the character is space for example.I wrote the code below but it doesn't work.
Expand|Select|Wrap|Line Numbers
  1. def c (file, char):
  2.     f = open ('file1.txt', 'r')
  3.     s = f.read()
  4.     i = 0
  5.     for char in s:
  6.         i += 1
Mar 12 '15 #1
5 4358
bvdet
2,851 Expert Mod 2GB
Maybe this will help. I have a string s with 2 commas in it.
Expand|Select|Wrap|Line Numbers
  1. >>> char = ","
  2. >>> i = 0
  3. >>> for letter in s:
  4. ...     if letter == char:
  5. ...         i+=1
  6. ...         
  7. >>> i
  8. 2
  9. >>> 
Return the character count i at the end of the function definition.
Expand|Select|Wrap|Line Numbers
  1.     return i
Mar 12 '15 #2
thanks. but I want a function and it should take a file not a string. How should I write a function?
Mar 12 '15 #3
bvdet
2,851 Expert Mod 2GB
It won't help you if I write the function for you. You already have a string assigned to s in the function you wrote. I gave you the for loop to count the characters. Substitute it for your for loop and add the return statement. BTW, anytime you create an open file object, it is good practice to close it when you are done. Otherwise you could read the file without creating a file object:
Expand|Select|Wrap|Line Numbers
  1. s = open('file1.txt').read()
Mar 12 '15 #4
Expand|Select|Wrap|Line Numbers
  1. def C (file, char):
  2.     f = open ('file1.txt', 'r')
  3.     s = f.read()
  4.     i = 0
  5.     for ch in s:
  6.         if ch == char:
  7.             i += 1
  8.     return i
Mar 12 '15 #5
bvdet
2,851 Expert Mod 2GB
thistle,

Please use code tags when posting code.

Don't forget the close the file object.
f.close()
Mar 12 '15 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: jester.dev | last post by:
Hello, I'm learning Python from Python Bible, and having some problems with this code below. When I run it, I get nothing. It should open the file poem.txt (which exists in the current...
5
by: Premshree Pillai | last post by:
Hello, I recently wrote a Perl version of pyAlbum.py -- a Python script to create an image album from a given directory -- plAlbum.pl . It made me realize how easy-to-use Python is. ...
1
by: Srijit Kumar Bhadra | last post by:
Hello, I see that it is possible to use mmapfile.pyd of win32all. The same is mentioned in http://www.python.org/windows/win32/#mmapfile. Unfortunately I could not trace any example using...
0
by: travis ray | last post by:
Hi, I have an extension in which a file object is created in python and passed down to a c extension which attempts to read from it or write to it. Writing to the file pointer seems to work...
12
by: Karlo Lozovina | last post by:
I've been Googling around for _small_, flat file (no server processes), SQL-like database which can be easily access from Python. Speed and perforamnce are of no issue, most important is that all...
1
by: upendrao | last post by:
Hi, I have a requirement in which I need to count the number of file descriptors associated with a file. To be more clear...A file can be opened by several processes say in read only mode. I would...
5
by: walterbyrd | last post by:
The strings start with whitespace, and have a '*' or an alphanumeric character. I need to know how many whitespace characters exist at the beginning of the string.
3
Elias Alhanatis
by: Elias Alhanatis | last post by:
Hello everybody!! Could you please inform me how can i add a sound file to an app. which animates cartoons ( ..or whatever..). I am working with Vista,Python2.5.1,IDLE,Tkinter. Thank you all in...
1
by: pcamps | last post by:
I currently have a list of genes in a file. Each line has a chromosome with it's information. Such an entry appears as: NM_198212 chr7 + 115926679 115935830 115927071 11593344 2...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.