无法正确读取文件中的行 [英] Unable to properly read the lines from a file

查看:71
本文介绍了无法正确读取文件中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文件,该文件是使用 python脚本编写的。该文件很大,包含1000多行,每行都非常大,看起来像是:(缩短)

I have file, which i wrote using a python script. The file is large and contain more than a 1000 lines, and each line is very large and it goes like :(shortened)

1 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
  1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
  1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
2 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
  1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
  1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
3 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
  1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
...

然后,每行可能的空间在监视器中显示3行。

当我尝试时:

And see, each line could take the space of 3 lines while displaying it in the monitor.
When I try :

fp = open('data.txt','r')
c = 0
for line in fp:
    c += 1
print("No. of line = ",c)

我得到正确的值,并且当我使用 read()函数,得到一个不同的值,例如:

I get the correct value, and when I use the read() function, I get a different value, as in:

fp = open('data.txt','r')
c = 0
data = fp.read()
for line in data:
    c += 1
print("No. of line = ",c)

有人可以解释一下,使用 read( )函数,而不使用它?

Can somebody explain, what is the difference between using the read() function, and not using it ?

预先感谢...

推荐答案

使用

data = fp.read()
for line in data:
    c += 1 

您可以全部读入一个字符串,而 for -loop将此字符串视为字符列表-这样就可以计算字符了。

you read all in one string and for-loop treats this string as list of chars - so you count chars.

您必须使用 readlines( )获取行列表并对该列表中的行进行计数

You have to use readlines() to get list of lines and count lines in this list

data = fp.readlines()
for line in data:
    c += 1 






BTW:计算字符的结果相同


BTW: The same result to count chars

data = fp.read()
c = len(data) 

并计算行数

data = fp.readlines()
c = len(data)






BTW:您还可以使用 print()来查看什么您有变量

data = fp.read()
print(data[0])
print(data[:3])
print(data)

data = fp.readlines()
print(data[0])
print(data[:3])
print(data)

如果要在一个脚本中进行测试,则必须关闭并再次打开失败,或者使用 fp.seek(0)移至文件开头,然后再次阅读。

If you want to test in the one script then you have to close and open fail again or use fp.seek(0) to move to beginning of file before you read again.

要使用行,应使用

fp = open('data.txt','r')

for line in fp:
    # ...code ...

fp.close()

fp = open('data.txt','r')
all_lines = fp.readlines()

for line in all_lines:
    # ...code ...

fp.close()

相同于 with ... as ...

with open('data.txt','r') as fp:
    for line in fp:
        # ...code ...

with open('data.txt','r') as fp:
    all_lines = fp.readlines()
    for line in all_lines:
        # ...code ...

这篇关于无法正确读取文件中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆