想单行打印csv数据。
CSVFile.txt:
abc|xyz|111
def|uvw|222
Python 3.x代码:
with open(csvfile, "r") as csvdata:
for line in csvdata:
fields = line.split("|")
for data in fields:
# do some processing and then
print(data)
这会将数据打印为
abc
xyz
111
def
uvw
222
要以
的格式直接打印数据abc, xyz, 111
def, uvw, 222
答案 0 :(得分:1)
您可以将已处理的data
存储在列表中,并在完成每一行的处理后将它们一起打印:
...
row = []
for data in fields:
# do some processing and then
row.append(str(data))
print(", ".join(row))
...