简体   繁体   English

Python:读写文件时的行终止

[英]Python: line termination while reading and writing files

I've written a simple utility which removes certain lines from a file. 我编写了一个简单的实用程序,可以从文件中删除某些行。 It reads a file into a list of lines (Python), and writes them back as a single concatenated string, new lines preserved; 它将文件读入行列表(Python),并将它们写为单个串联字符串,并保留新行; some lines are dropped, some get commented in the process; 一些行被删除,一些在该过程中被注释; the percentage of change is negligible. 变化的百分比可以忽略不计。 Somehow diff presents me with a big red block before , and a big green block after . 不知怎的,DIFF送给我一份大红色块之前经过一个大的绿色块。 To a naked eye the resulting file looks quite good; 肉眼可见,生成的文件看起来非常不错。 I thought about some subtle difference with the tailing spaces or something like that, but is it really possible? 我想到了拖尾空间或类似的东西之间的细微差别,但是真的有可能吗? Had I added something invisible to each line, every red line would have been followed by the corresponding green one. 如果我在每行中添加了不可见的内容,则每条红线之后都会有相应的绿线。 Or so I gather. 还是我聚集。

UPD: UPD:

Well, line endings is a certainty, I was told. 有人告诉我,行尾是确定的。 The essentials of my code: 我的代码的要点:

def check_file(path):
    out_line = ""
    with open(path, "r") as f_r:
        for line in f_r.readlines():
            drop_it, o_line = consume_line(line)
            if drop_it:
                pass
            else:
                out_line += o_line
    with open(path, "w") as f_w:
        f_w.write(out_line)

consume_line() essentially returns its argument as is. consume_line()本质上按原样返回其参数。 It may be either scheduled for dropping, or uncommented/commented out, C++ style, in certain infrequent cases. 在某些不常见的情况下,可以计划将其丢弃,也可以不进行注释/注释。 No manual fiddling with line endings in any case. 在任何情况下,都无需手动摆弄行尾。

No editor reports any change in the total number of lines if no line is dropped. 如果没有删除任何行,则没有编辑器报告行总数的任何变化。 The files originated and handled on Linux. 这些文件起源于Linux。

This code sets all the line endings after the first eol in the file. 此代码设置文件中第一个eol之后的所有行尾。 Trailing spaces preserved. 保留尾随空间。

g_newline = ''


def current_nl(f):
    global g_newline

    if g_newline:
        return g_newline
    g_newline = f.newlines
    if isinstance(g_newline, tuple):
        print('!!! Something wrong. This is supposed to be the first eol in the file\n')
        return '\n'
    return g_newline


def check_file(path):
    global g_newline

    g_newline = ''
    out_line = ""

    with open(path, "r") as f_r:
        for line in f_r.readlines():
            drop_it, o_line = consume_line(line)
            if drop_it:
                pass
            else:
                out_line += o_line.rstrip('\n') + current_nl(f_r)
    with open(path, "w") as f_w:
        f_w.write(out_line)

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

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