简体   繁体   English

读取两行值并使用python写入一行

[英]Reading values of two lines and write in one line with python

I have a file with this format: 我有一个具有这种格式的文件:

611    2856     618    2582   94075   94107   94065   94068
101071   94104
598    2856     618     611   93995   94107   93992   93991
94075   94065
612    2834    2821    2812   94087  101577   94085   94081
101558  101557

I need to read the lines and to rewrite them so: 我需要阅读这些行并重写它们,以便:

611    2856     618    2582   94075   94107   94065   94068 101071   94104
598    2856     618     611   93995   94107   93992   93991 94075   94065
612    2834    2821    2812   94087  101577   94085   94081 101558  101557

I have tried something like this: 我已经尝试过这样的事情:

f2 = open('new_file.txt', 'w')
    f1 = open('old_file.txt')
    lines = f1.readlines()
    for i, line in enumerate(lines):    
        print(repr(line))               
        f2.write(line)
        i = i+1
    f1.close()
f2.close()

But it is not working as it writes again every line. 但是它不起作用,因为它在每一行都再次写入。 I need to read two lines and write them in one. 我需要阅读两行,然后将它们写成一行。 Any suggesitons? 有建议吗?

You don't need to read the lines into a list, or mess about with line numbers, you can just iterate directly over the file lines: 您无需将行读入列表中,也无需弄乱行号,只需直接在文件行上进行迭代即可:

with open('oldfile.txt') as fin, open('newfile.txt', 'w') as fout:
    for line in fin:
        fout.write(line[:-1] + ' ' + next(fin))    

contents of newfile.txt newfile.txt的内容

611    2856     618    2582   94075   94107   94065   94068 101071   94104
598    2856     618     611   93995   94107   93992   93991 94075   94065
612    2834    2821    2812   94087  101577   94085   94081 101558  101557

line[:-1] + ' '

removes the newline char at the end of the long lines, and replaces it with a single space; 删除长行末尾的换行符,并用单个空格替换; I'm pretty sure that this is faster than doing line.replace('\\n', ' ') , but I haven't timed it. 我很确定这比执行line.replace('\\n', ' ')快,但是我还没有计时。

As with Graipher's solution, if the file doesn't have an even number of lines, then the last line will not be copied, but I assume that's not an issue for your data. 与Graipher的解决方案一样,如果文件没有偶数行,则不会复制最后一行,但是我认为这对您的数据来说不是问题。

The lines returned by readlines still contain the newline '\\n' at the end . readlines返回的行最后仍包含换行符'\\n' You must strip them: 您必须strip它们:

with open('new_file.txt', 'w') as f2:
    with open('old_file.txt') as f1:
        lines = f1.readlines()
        # lines = f1.read().splitlines()  will even save you the stripping
        for i, line in enumerate(lines):  # line: 'foo bar \n'         
            f2.write(line.strip())
            if i % 2:  # linebreak only every other line
                f2.write('\n')

You can use a buffer variable to store every second line like so: 您可以使用缓冲区变量存储第二行,如下所示:

with open('new_file.txt', 'w') as fout:
    with open('old_file.txt') as fin:
        buffer = ""
        for i, line in enumerate(fin):
            if i % 2 == 0:
                buffer = line.replace('\n', '')
            else:
                fout.write(sep.join((buffer, line)))
                buffer = ""

With sep = " " or whatever else you use for separation. 使用sep = " "或其他用于分隔的内容。

This assumes you have an even number of lines, otherwise the last line will not be written. 假设您有偶数行,否则将不会写入最后一行。

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

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