python - 为什么文本文件行中的 '\x01\x1A'( header 开始和替代控制字符)会过早地停止 for 循环?

标签 python for-loop readlines control-characters

我正在使用 Python 2.7.15、Windows 7

上下文

我编写了一个脚本来读取 FileZilla 日志文件(规范 here)的每一行并将其标记为发起与 FileZilla 服务器的连接的主机的 IP 地址。我在解析 > 字符后的 log text 字段时遇到问题。我写的脚本使用了:

    with open('fz.log','r') as rh:
       for lineno, line in rh: 
          pass

构造以读取每一行。该 for 循环在遇到包含 SOHSUB 字符的 log text 字段时过早停止。我无法向您显示日志文件,因为它包含敏感信息,但问题的关键可以通过读取包含这些字符的文本文件来重现。

我的目标是提取 IP 地址(我可以使用 re.search() 来完成)但在此之前,我必须删除那些控制字符。为此,我创建了一份日志文件副本,其中包含这些控制字符的行已被删除。可能有更好的方法,但我更好奇为什么 for 循环在遇到控制字符后就停止了。

重现问题

我用这段代码重现了这个问题:

if __name__ == '__main__':
    fn = 'writetest.txt'
    fn2 = 'writetest_NoControlChars.txt'

    # Create the problematic textfile
    with open(fn, 'w') as wh: 
        wh.write("This line comes first!\n");
        wh.write("Blah\x01\x1A\n"); # Write Start-of-Header and Subsitute unicode character to line
        wh.write("This comes after!")

    # Try to read the file above, removing the SOH/SUB characters if encountered
    with open(fn, 'r') as rh:
        with open(fn2, 'w') as wh:
            for lineno, line in enumerate(rh):
                sline = line.translate(None,'\x01\x1A')
                wh.write(sline)
                print "Line #{}: {}".format(lineno, sline)
    print "Program executed."

输出

上面的代码创建了 2 个输出文件并在控制台窗口中生成以下内容:

Line #0: This line comes first!

Line #1: Blah
Program executed.

我在 Eclipse 中逐步调试代码,并在执行

for lineno, line in enumerate(rh): 

语句,rh,打开的文件的句柄已关闭。我原以为它会移到第三行,打印出 This comes after! 到控制台并将其写到 writetest_NoControlChars.txt 但两个事件都没有发生。相反,执行跳转到 print "Program executed"Picture of Local Variable values in Debug Console

最佳答案

如果您知道此文件包含非文本数据,则必须以二进制模式打开此文件:open(fn, 'rb')

关于python - 为什么文本文件行中的 '\x01\x1A'( header 开始和替代控制字符)会过早地停止 for 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53112537/

相关文章:

C For 循环不起作用?

javascript - 仅使用最后一个数组项动态生成的内容

c++ - 在迭代过程中是否会访问在std::unordered_set(或unordered_map)中添加的元素?

python - 使用UTF-8打开文件进行读取

python - 通过向量的差异对矩阵创建进行向量化(例如,对于 numpy)

python - 使用 python 队列进行 Django Rest Framework 测试

python - 当数据帧包含混合数据类型时,Pyarrow from_pandas 会使解释器崩溃

python - 将utc时间添加到文件名python

java - readLines() 上的 Groovy Reader HashSet 返回乱序

python - 这条线在错误的地方 split