简体   繁体   English

从文本文件中读取多行

[英]Reading multiple lines from text file

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我可以用 java 轻松完成,但我们需要使用 python,而且我对 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'])

I am not sure what lines is from your code, you are assigning the list containing lines from the text into data variable.我不确定您的代码中的哪些lines ,您正在将包含文本行的列表分配给data变量。 If you want to read the whole text as a single string, then just use read() method.如果要将整个文本作为单个字符串read() ,则只需使用read()方法。

from collections import Counter

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

counter = Counter(data)

print (counter['a'])
print (counter['b'])
...

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.我已经解决了这个问题(新的完整代码如下)并且正在“工作”,我需要根据我的 Java 代码结果检查它。 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

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM