如何计算多个 .xyz 文件的简单加法?
How can I compute simple addition on multiple .xyz files?
我目前正在尝试编写一个 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()
在你的函数中:
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() 已经这样做了)。
What your code does: parts1 = "1 1 1"
What you want it to do: parts1 = ["1", "1", "1"]
用 .split()
分割第 1 行和第 2 行,改为在所有空格上分割。
编辑:您还需要将写入输出文件的部分缩进到循环内,因为 parts1 和 parts2 每次都会被覆盖。尝试以类似于此的方式统一所有代码:
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')
你的循环算法是错误的。你想要的是逐行解析 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)
我会这样做:
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
我目前正在尝试编写一个 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()
在你的函数中:
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() 已经这样做了)。
What your code does:
parts1 = "1 1 1"
What you want it to do:
parts1 = ["1", "1", "1"]
用 .split()
分割第 1 行和第 2 行,改为在所有空格上分割。
编辑:您还需要将写入输出文件的部分缩进到循环内,因为 parts1 和 parts2 每次都会被覆盖。尝试以类似于此的方式统一所有代码:
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')
你的循环算法是错误的。你想要的是逐行解析 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)
我会这样做:
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