python 读写文件 另存为_python如何读取tsv文件,将其清除并另存为新文件?

您不需要编写任何更改,您只需更改每行中的第五个元素,而不需要对其执行任何操作,如果您要更改原始文件,可以将其写入tempfile,并执行shutil.move以用更新的temp替换原始文件:import string

exclude = string.punctuation

from tempfile import NamedTemporaryFile

from shutil import move

with open("test1") as tsvfile, NamedTemporaryFile(dir=".",delete=False) as t:

tsvreader = csv.reader(tsvfile, delimiter="\t")

temp = csv.writer(t,delimiter="\t")

for row in tsvreader:

row[4] = row[4].strip(exclude)

temp.writerow(row)

move(t.name,"test1")

如果要创建新文件而不是更新原始文件,只需打开一个新文件并写入每个已清理的行:with open("test1") as tsvfile, open("out","w") as t:

tsvreader = csv.reader(tsvfile, delimiter="\t")

temp = csv.writer(t,delimiter="\t")

for row in tsvreader:

row[4] = row[4].strip(exclude)

temp.writerow(row)

去掉标点符号str.strip(exclude)就足够了。如果要从任何位置删除,可以返回到''.join([ch for ch in line[4] if ch not in exclude]),但如果要从任何位置删除,则应使用str.translate:row[4] = row[4].translate(None,exclude)

如果要添加空格:from string import maketrans

tbl = maketrans(exclude," "*len(exclude))

....

row[4] = row[4].translate(tbl)

最后,如果您实际上是指第四列,那么它应该是row[3]而不是row[4]


版权声明:本文为weixin_32451529原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。