-2

The goal for my project is to count all the characters in a text file. My code seems to work, but seems it will only ready the first line of code from the file and nothing else. I can easily do it in java, but we are required to use python and I have very little experience working with python

my code is as follows:

import pandas as pd

from collections import Counter

with open ('myFile', "r") as myfile:
   data=myfile.readlines()

counter = Counter(lines)

print (counter['a'])
print (counter['b'])
print (counter['c'])
print (counter['d'])
print (counter['e'])
print (counter['f'])
print (counter['g'])
print (counter['h'])
print (counter['i'])
print (counter['j'])
print (counter['k'])
print (counter['l'])
print (counter['m'])
print (counter['n'])
print (counter['o'])
print (counter['p'])
print (counter['q'])
print (counter['r'])
print (counter['s'])
print (counter['t'])
print (counter['u'])
print (counter['v'])
print (counter['w'])
print (counter['x'])
print (counter['y'])
print (counter['z'])
mateo8
  • 11
  • Your code is reading the lines (not characters) of the file to a variable called ``data``, but the counter operates on an undefined variable called ``lines``. This code will definitely not work. If you have some code that "seems to work", please post it alongside some example input to reproduce your problem. – MisterMiyagi Mar 15 '20 at 18:58
  • Many examples of this on the net. See [this](https://stackoverflow.com/a/8010133/10682164) answer. – totalhack Mar 15 '20 at 19:00
  • Please provide a [mcve]. This is a topic on which there is plenty of information available, what is different about this particular case? – AMC Mar 15 '20 at 20:25
  • Welcome @mateo8, in your code, you can print(data) to see what you are actually getting. A simple and not so clever way is to loop through the lines and count each character. Of course, there are many cleverer ways in python to do that without looping. – Nicholas TJ Mar 15 '20 at 20:44

3 Answers3

0

I am not sure what lines is from your code, you are assigning the list containing lines from the text into data variable. If you want to read the whole text as a single string, then just use read() method.

from collections import Counter

with open ('myFile', "r") as myfile:
   data = myfile.read()

counter = Counter(data)

print (counter['a'])
print (counter['b'])
...
ywbaek
  • 2,971
  • 3
  • 9
  • 28
0

I realized that the code I posted was from two editors I had open at the same time with the same version number, which caused the errors. I have fixed that (new complete code is below) and is "working" I need to check it against my java code results. I have included the results of the code

import pandas as pd

from collections import Counter



data = open('myFile.txt').read()
data.lower()
print (data)


counter = Counter(data)

print (counter['a'])
print (counter['b'])
print (counter['c'])
print (counter['d'])
print (counter['e'])
print (counter['f'])
print (counter['g'])
print (counter['h'])
print (counter['i'])
print (counter['j'])
print (counter['k'])
print (counter['l'])
print (counter['m'])
print (counter['n'])
print (counter['o'])
print (counter['p'])
print (counter['q'])
print (counter['r'])
print (counter['s'])
print (counter['t'])
print (counter['u'])
print (counter['v'])
print (counter['w'])
print (counter['x'])
print (counter['y'])
print (counter['z'])

results from my updated code:

28042
3064
9280
12192
45763
3434
5143
3523
24633
1107
328
16641
8817
23809
18195
9202
3611
23139
25219
25990
19636
5302
465
1396
1257
473
mateo8
  • 11
-1

Have you tried something like the following?

file = open("myFile.txt", "r")
data = file.read()
number_of_characters = len(data)
print('Number of characters in text file : {}'.format(number_of_characters))
file.close()
redmonkey
  • 86
  • 9
  • Would like to know why this comment got downvoted? The user clearly wanted a method to count all of the characters inside of the text file? – redmonkey Mar 15 '20 at 19:06
  • The question code shows counting individual letters is desired, not the total of characters. Manually closing files instead of using ``with`` is bad practice, as it may leave files open indefinitely. There is no need to read the entire file into memory at once, reading line-wise or in other increments also works on very large files. – MisterMiyagi Mar 15 '20 at 19:10
  • Sure, totally agree with the use of "with" as a context manager. It wasn't entirely clear to me that the user wanted to do this on a line by line basis; the first sentence was "The goal for my project is to count all the characters in a text file." - I thereby merely provided a basic solution. Thanks anyway! – redmonkey Mar 15 '20 at 19:15