繁体   English   中英

Python中的readline()错误

[英]Wrong readline() in Python

我有一个readline()方法的问题,它有时返回2行而不是一行,我不知道为什么。 有人能帮我吗 ?

这里是我读过的文本文件的一部分(带记事本):

at+gpsinit=2
OK

+GPSEVINIT: 1
at+gpsnmea=3
OK
at+gpsstart=0
OK

并使用Notepad ++:

at+gpsinit=2CR
CR LF
OKCR LF
CR LF
+GPSEVINIT: 1CR LF
at+gpsnmea=3CR
CR LF
OKCR LF
at+gpsstart=0CR
CR LF
OKCR LF

这是我在Python shell中得到的:

16 : at+gpsinit=2

17 : 

18 : OK

19 : 

20 : +GPSEVINIT: 1

21 : at+gpsnmea=3

在这里我的代码:

# Open a file
file = open("testtxt.txt", 'r')
line = 0

for current_line in file:
    line += 1    
    print(str(line)+" : "+current_line)

# Close opend file
file.close()

您遇到的问题很可能是由于行尾标记出现问题。

  • Windows / Dos通常使用CRLF (或, \\r\\n ,或以字节为单位的0d0a )。
  • Unix通常使用LF (或\\n ,或字节为0a
  • MacOS通常使用CR (或\\r或字节为0d

以下是ASCII编码文件的一些示例:

$ hexdump -C test_dos.txt
00000000  68 65 6c 6c 6f 0d 0a 77  6f 72 6c 64 0d 0a        |hello..world..|
0000000e

$ hexdump -C test_nix.txt
00000000  68 65 6c 6c 6f 0a 77 6f  72 6c 64 0a              |hello.world.|
0000000c

$ hexdump -C test_mac.txt
00000000  68 65 6c 6c 6f 0d 77 6f  72 6c 64 0d              |hello.world.|
0000000c

广告你可以看到,单词hello68 65 6c 6c 6f )后面跟着不同的字节0d 0a0a0d 在MS-Notepad中编辑文件时,很可能会插入CRLF 由于LF在软件开发中最常见,因此Notepad ++最有可能添加这些。

现在,对于您的代码:鉴于上面的三个文件,与您类似的代码产生以下结果:

码:

files = ('test_dos.txt', 'test_nix.txt', 'test_mac.txt')

for fname in files:
    print("Reading {}".format(fname))
    with open(fname) as fptr:
        for line in fptr:
            print("--> {!r}".format(line))
    print(80*"-")

输出:

Reading test_dos.txt
--> 'hello\r\n'
--> 'world\r\n'
--------------------------------------------------------------------------------
Reading test_nix.txt
--> 'hello\n'
--> 'world\n'
--------------------------------------------------------------------------------
Reading test_mac.txt
--> 'hello\rworld\r'
--------------------------------------------------------------------------------

正如您可以清楚地看到的,Python在\\n字符上分割,但不会从输出中删除它。 这就是为什么“mac”示例只有一行。

如果您必须处理来自异类源的文件,请考虑使用U标志激活“通用换行符”以打开

这是一个例子。 请注意,唯一改变的是要openU参数:

files = ('test_dos.txt', 'test_nix.txt', 'test_mac.txt')

for fname in files:
    print("Reading {}".format(fname))
    with open(fname, 'U') as fptr:
        for line in fptr:
            print("--> {!r}".format(line))
    print(80*"-")

输出:

Reading test_dos.txt
--> 'hello\n'
--> 'world\n'
--------------------------------------------------------------------------------
Reading test_nix.txt
--> 'hello\n'
--> 'world\n'
--------------------------------------------------------------------------------
Reading test_mac.txt
--> 'hello\n'
--> 'world\n'
--------------------------------------------------------------------------------

如您所见,并非所有文件的行为都相同。 这可能提示您辣椒U在那里你正在阅读的文本文件随处可见。 但是,我确信有一个很好的理由说明它不是默认的! :)

readline()例程肯定没有错误; 太多人经常使用它,除非你有一个非常奇怪的实现,而不是标准的Python,你也会使用一个不错的版本。

您提供的信息还不足以确定您的问题的原因是什么,但有一些分析方法我会建议您找出您正在处理的内容。

你应该仔细看看你的行中有什么,哪些字节终止你的行( '\\n''\\r\\n'或者其他什么)并且特别仔细看看at+gpsinit=2处的行和它的行结束。

在Unix系统上,您可以使用od (或xxd )。 使用选项-c打印字符。 使用-t x1 -tc也可以获得每个字节的十六进制输出。

好的,所以我解决了我的问题,似乎Np给了我错误的文本文件。 无论如何我用这个命令:

file = open("testtxt.txt", 'r', newline="\r\n")

它给了我好的台词。

暂无
暂无

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

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