简体   繁体   English

将表格保存在 .txt 文件中

[英]Saving table in a .txt file

I was wondering if you could help me with a rather simple bit of code that I got stuck on.我想知道您是否可以帮助我解决一些我陷入困境的相当简单的代码。

I have a table in .txt that looked like this:我在 .txt 中有一个表,如下所示:

FileName文档名称 predOGT (C) predOGT (C)
CTOTU-1_1.fasta CTOTU-1_1.fasta 31.30 31.30
CTOTU-2_1.fasta CTOTU-2_1.fasta 21.39 21.39
CTOTU-0_1.fasta CTOTU-0_1.fasta 11.31 11.31
CTOTU-2_5.fasta CTOTU-2_5.fasta 42.32 42.32
CTOTU-2_2.fasta CTOTU-2_2.fasta 34.33 34.33
CTOTU-1_2.fasta CTOTU-1_2.fasta 35.34 35.34
CTOTU-0_3.fasta CTOTU-0_3.fasta 23.35 23.35
CTOTU-1_3.fasta CTOTU-1_3.fasta 64.36 64.36
CTOTU-2_4.fasta CTOTU-2_4.fasta 33.37 33.37
CTOTU-2_3.fasta CTOTU-2_3.fasta 27.38 27.38

Which I have successfully changed to look like this:我已成功更改为如下所示:

OldFile旧文件 NewFile新文件 predOGT_(C) predOGT_(C)
CTOTU-1 CTOTU-1 1 1 31.30 31.30
CTOTU-2 CTOTU-2 1 1 21.39 21.39
CTOTU-0 CTOTU-0 1 1 11.31 11.31
CTOTU-2 CTOTU-2 5 5 42.32 42.32
CTOTU-2 CTOTU-2 2 2 34.33 34.33
CTOTU-1 CTOTU-1 2 2 35.34 35.34
CTOTU-0 CTOTU-0 3 3 23.35 23.35
CTOTU-1 CTOTU-1 3 3 64.36 64.36
CTOTU-2 CTOTU-2 4 4 33.37 33.37
CTOTU-2 CTOTU-2 3 3 27.38 27.38

Now I would like to save this new table to a new .txt table现在我想将这个新表保存到一个新的 .txt 表中

Below is my code which so far allows only the last row to be saved in the new_file.txt .下面是我的代码,到目前为止只允许将最后一行保存在new_file.txt

Could you tell me what mistake I am doing?你能告诉我我在做什么错误吗?

filepath = 'original_file.txt'
new_filepath = 'new_file.txt'

with open(filepath, 'r+') as f:
    for line in f:
        new_line = line.strip()
        new_line = new_line.replace('_','\t').replace('FileName','OldFile\tNewFile').replace('predOGT (C)','predOGT_(C)').replace('.fasta','').replace('CTOTU-','')
        print(new_line)
        with  open(new_filepath, "w") as n:
            new = n.writelines(new_line)

By doing that通过这样做

filepath = 'original_file.txt'
new_filepath = 'new_file.txt'

with open(filepath, 'r+') as f:
    for line in f:
        new_line = line.strip()
        new_line = new_line.replace('_','\t').replace('FileName','OldFile\tNewFile').replace('predOGT (C)','predOGT_(C)').replace('.fasta','').replace('CTOTU-','')
        print(new_line)
        with  open(new_filepath, "w") as n:
            new = n.writelines(new_line)

you are opening new_filepath for each for -loop turn in write ( w ) mode, which does remove existing content from it.您在写入( w )模式下为每个for循环打开 new_filepath ,这确实从中删除了现有内容。 If you want to added further data to existing file without erasing you need to use append ( a ) mode.如果您想在不删除的情况下向现有文件添加更多数据,则需要使用 append ( a ) 模式。 Please run following snippets and observe what would happend in each case, first using w mode请运行以下代码段并观察每种情况下会发生什么,首先使用w模式

with open("file1.txt","w") as f:
    f.write("uno\n")
with open("file1.txt","w") as f:
    f.write("dos\n")
with open("file1.txt","w") as f:
    f.write("tres\n")

then using a mode然后使用a模式

with open("file2.txt","a") as f:
    f.write("uno\n")
with open("file2.txt","a") as f:
    f.write("does\n")
with open("file2.txt","a") as f:
    f.write("tres\n")

For me it worked like this:对我来说,它是这样工作的:

n = open(new_filepath, "w")

with open(filepath, 'r+') as f:
    for line in f:
        print(new_line)
        n.writelines(new_line)

The new_filepath is opened before and then the indentation should be the same as the print() one to add every line that the loop goes through. new_filepath 在之前打开,然后缩进应该与 print() 相同,以添加循环通过的每一行。

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

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