我想从pList.csv
文件中读取所有内容并将其写入一个字符串中,以便每一行都用逗号分隔。
该文件只有一列。例如,pList.csv
是:
28469977
24446384
25968054
并且输出字符串必须是: 28469977,24446384,25968054
为此,请考虑以下代码。但是有一点问题
p_list = ""
with open("pList.csv", mode="r") as infile:
reader = csv.reader(infile)
for row in reader:
p_list += row[0]
if its_not_last_loop :
p_list += ","
什么表达式适用于its_not_last_loop
它,
而不应用于文件的最后一行?
尝试这个:
with open("pList.csv", mode="r") as infile:
reader = csv.reader(infile)
out_list = []
for row in reader:
out_list.append(row[0]) #row[0] get value for the sample input
p_list = ",".join(out_list)
print(p_list)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句