我目前正在尝试编写一个Python程序,该程序将包含2个现有的.xyz文件(包含由空格分隔的3列值),循环遍历它们,从列表的每列中的等效位置添加数字,然后编写这些值在同一位置的单独.txt文件中。
例如,如果我有列表:
(line denoting number of atoms)
(comment line)
(atom symbol) 1 1 1
(atom symbol) 2 2 2
(atom symbol) 3 3 3
和列表:
(line denoting number of atoms)
(comment line)
(atom symbol) 3 3 3
(atom symbol) 2 2 2
(atom symbol) 1 1 1
我想让python以以下形式创建一个新文件:
(line denoting number of atoms)
(comment line)
(atom symbol) 4 4 4
(atom symbol) 4 4 4
(atom symbol) 4 4 4
目前我正在给我错误:
TypeError: float() argument must be a string or a number
我的代码是:
def sumen():
infile_1 = input("Name of first file you wish to open as file using '.txt': ")
infile_2 = input("Name of second file you wish to open as file using '.txt': ")
outfile = input("Name the output file using '.txt': ")
with open(infile_1, 'r') as myfile_1, open(infile_2, 'r') as myfile_2:
for line1 in myfile_1.readlines():
parts1 = line1.split('\n')
for line2 in myfile_2.readlines():
parts2 = line2.split('\n')
with open(outfile, 'w') as outputfile:
totalenergy = float(parts1) + float(parts2)
print("The output was printed to:", outfile)
sumen()
答案 0 :(得分:0)
在你的功能中:
with open(infile_1, 'r') as myfile_1, open(infile_2, 'r') as myfile_2:
for line1 in myfile_1.readlines():
parts1 = line1.split('\n')
for line2 in myfile_2.readlines():
parts2 = line2.split('\n')
您需要按空格分割线条,因为它只会拆分换行符号( .readlines()已经存在)。
您的代码的作用:
parts1 = "1 1 1"
您希望它做什么:
parts1 = ["1", "1", "1"]
将line1和line2与.split()
分开,以便在所有空格上拆分。
编辑:您还需要将输出文件的写入部分缩进到循环内部,因为每次都会覆盖parts1和part2。尝试以类似于此的方式统一所有代码:
with open(infile_1, 'r') as myfile_1, open(infile_2, 'r') as myfile_2, open(outfile, 'w') as outputfile:
for line1, line2 in zip(myfile_1.readlines(), myfile_2.readlines()):
parts1 = line1.split()
parts2 = line2.split()
totalenergy = []
for part1, part2 in zip(parts1, parts2):
totalenergy.append(float(part1) + float(part2))
outputfile.write(str(totalenergy) + '\n')
答案 1 :(得分:0)
您的循环算法错误。你想要的是逐行解析file1和file2,并在移动到下一行之前一次处理每个文件的一行。
我建议你像这样重写:
with open(infile_1, 'r') as myfile_1, open(infile_2, 'r') as myfile_2, open(outfile, 'w') as myfile_3:
f3 = []
for (line1, line2) in zip(myfile_1.readlines(), myfile_2.readlines()):
values1 = line1.split()[1:]
values2 = line2.split()[1:]
f3.append(' '.join([str(float(x) + float(y)) for x, y in zip(values1, values2)])+'\n')
pass
myfile_3.writelines(f3)
答案 2 :(得分:0)
这是我将如何做到的:
def sumen():
infile_1 = input("Name of first file you wish to open as file using '.txt': ")
infile_2 = input("Name of second file you wish to open as file using '.txt': ")
outfile = input("Name the output file using '.txt': ")
with open(infile_1, 'r') as myfile_1, \
open(infile_2, 'r') as myfile_2, \
open(outfile, 'w') as outputfile:
for _ in range(2): # copy first two lines of first file (and advance second)
outputfile.write(next(myfile_1))
next(myfile_2)
for (line1, line2) in zip(myfile_1, myfile_2):
parts1 = line1.split()
parts2 = line2.split()
totalenergy = [float(parts1[i]) + float(parts2[i]) for i in range(1, len(parts1))]
outputfile.write('{} {}\n'.format(parts1[0], ' '.join(map(str, totalenergy))))
print("The output was printed to:", outfile)
sumen()
对于您的示例输入文件,这将生成一个如下所示的输出文件:
(line denoting number of atoms)
(comment line)
(atom symbol) 4.0 4.0 4.0
(atom symbol) 4.0 4.0 4.0
(atom symbol) 4.0 4.0 4.0