+ 2
Reading a txt file in python and count its character quantities
Firstly you may ignore personal comments of line 1 to 7 This code (from line.10 to 26) is for receiving a string and returning a python dictionary contains name of character as the key and quantity of that character as the value Now I want to know how to input the string from a .txt file instead of input inside code ? I want to use code of line.29 to 32, but I don't know how to do it ... https://code.sololearn.com/cy466Vu9D7Df/?ref=app
4 Respostas
+ 5
# line by line approach
D = dict()
with open(filename, “r”) as F:
line = F.readline().strip()
count = 1
while line:
if count > 7:
for char in line:
if char in D.keys():
D[char] += 1
else:
D[char] = 1
line = F.readline().strip()
count += 1
+ 2
https://code.sololearn.com/cPQoyCd9l002/?ref=app
does this do what you want it to?
+ 1
I think the problem is that you are trying to use a code in sololearn playground to open a file in your computer drive.
It will not work like that, because your code is in a sololearn server in the internet.
For it to work your computer had to be serving the file to the internet.
And you needed the server ip address and port.
An easyer way to solve this problem is not using your on computer but a dedicated server (or the cloud if you will) for that.
EDITED:
I'm not sure, but I think I've seen someone use his google drive account to do that.
try searching the codes in the playground and I'm sure you will find a code of someone who already solved this problem.
+ 1
Lines 10 to 26 are constant, the only part I want to replace is line 24 with a code to read string from a txt file, not like present code (from a given string inside " " )