473,385 Members | 1,397 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,385 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 4362
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.