Python读取文件包含列中的数字,并将这些数字用于命令



我正在尝试用python编写脚本。我有一个.txt文件,其中包含具有此格式的数字(示例)198900.000的列。这个想法是一个一一逐一阅读这些数字,将它们用作命令的输入:

  1. python读取第一个数字;
  2. python使用该数字作为命令中的输入;
  3. 该命令写入一个命名的输出,例如," output_number"命令应迭代运行,直到列的结尾(行数为未知)。

你能帮我吗?谢谢!

list.txt:

600.000
630.000
640.000
650.000
660.000
680.000
690.000
720.000
740.000
750.000
770.000
780.000
800.000
810.000
820.000
830.000
840.000
850.000
860.000
3310.000

也许

print(open("list.txt").read().split())

输出

['600.000', '630.000', '640.000', '650.000', '660.000', '680.000', '690.000', '720.000', '740.000', '750.000', '770.000', '780.000', '800.000', '810.000', '820.000', '830.000', '840.000', '850.000', '860.000', '3310.000']

with open("list.txt","r") as f:
    lines = f.readlines()
    for x in lines:
        print("output_number: {}".format(x))

输出

output_number: 600.000
output_number: 630.000
output_number: 640.000
output_number: 650.000
output_number: 660.000
.
.
.

编辑

op python必须读取firts编号,并且对于firts编号,请执行命令。例如:python读取firts编号'600.00',然后做(我编写命令,所以可以清楚地清楚)' gmx trjconv -dump 600.00 -output dump_600.00'。然后,Python必须重复列中存在的所有数字

中的所有数字
commands_list = { 'clear': ' gmx trjconv -dump XxX -output dump_XxX '}
def callCommand(x):
    cmd = input("Enter command:")
    if cmd in commands_list:
        print(commands_list[cmd].replace("XxX", x))
    else:
        print("Command does not exit, quiting")
        exit()

with open("list.txt","r") as f:
    lines=f.readlines()
    for x in lines:
        callCommand(x)
        print("output_number: {}".format(x))

输出

Enter command:clear
 gmx trjconv -dump 600.000
 -output dump_600.000
output_number: 600.000
Enter command:blahhh
Command does not exit, quiting

假设您只有一个带有数字的一列。

如果您有多个列,则可以使用fieldnames属性来查明您的列。

import csv
output_number=list()
with open('yourfiledirectoryyourfile.txt', 'r') as f:
    df=csv.reader(f)
    for row in df:
       output_number.append(row)  

最新更新