嗨我有这样的数据
40
50
70
我想要这样的输出: 40,50,70
我试过了:
from sys import argv
script, pos_file, output = argv
positions = []
with open(pos_file) as f:
for x in f:
positions.append(x)
print positions
它为我提供了一个类似于' 40 \ n',' 50 \ n',' 70 \ n'所以它将数字视为字符串并打印行分隔符\ n。如何修改此代码以执行我想要的操作?
感谢。
答案 0 :(得分:0)
我已经为自己想出了这个问题,因为它可以帮助其他人(通过将我的列视为csv文件而略有欺骗):
from sys import argv
import csv
script, pos_file, output = argv
positions = []
out = open(output, "w")
with open(pos_file, "rb") as csv_file:
f = csv.reader(csv_file)
for x in f:
positions.append(x[0])
print ",".join(positions)
out.write(",".join(positions))
答案 1 :(得分:0)
您可以使用原始代码。您需要添加一行,以删除换行符。
from sys import argv
script, pos_file, output = argv
positions = []
with open(pos_file) as f:
for x in f:
x.rstrip("\n");
positions.append(x)
print positions
最后一行打印一个列表。在第二个代码中,您创建了一个字符串而不是列表。