我遇到在文件中每行的开头和结尾添加字符并在其间添加“,”的问题。
该文件包含一组点:
657648.688 4772318.920 133.542
657650.269 4772318.841 133.578
因此编辑完文件后,内容应如下所示:
[657648.688, 4772318.920, 133.542]
[657650.269, 4772318.841, 133.578]
感谢您的帮助。
答案 0 :(得分:1)
with open("input.txt") as infile, open("output.txt", "w") as outfile:
for line in infile:
outfile.write("[" + ", ".join(line.strip().split()) + "]\n")