我是python的新手,面临一些问题。 我有一个文件,其中两列用逗号分隔,具有数值(样本) -
2306,12017
2307,9416.6
2308,7378.5
2309,5781.4
依旧......
我愿意有一个像这样的输出文件 -
bfe,2306,hgen,,12017
bfe,2307,hgen,,9416.6
bfe,2308,hgen,,7378.5
bfe,2309,hgen,,5781.4
依旧......
提前感谢。
这是我到目前为止所尝试的,
data = open("myFile1.txt", "r").readlines()
output = ["%s,%s,%s,%s" % ("bfe",item.strip(),"hgen",item.strip()) for item in data]
f = open("output.txt", "w")
f.write("\n".join(output))
f.close()
答案 0 :(得分:0)
data = open("myFile1.txt", "r").readlines()
output = ["%s,%s,%s,%s" % ("bfe",item.split(",")[0],"hgen",item.split(",")[1]) for item in data]
f = open("output.txt", "w")
f.write("".join(output))
f.close()
Output.txt的
bfe,2306,hgen,12017
bfe,2307,hgen,9416.6
bfe,2308,hgen,7378.5
bfe,2309,hgen,5781.4