[英]Writing a list of strings of comma separated values into a CSV
I'm reading and parsing lines from a flat text file. 我正在读取和解析纯文本文件中的行。 I take certain values from that file, do some minor formatting/conversion steps, and spit out a comma separated string.
我从该文件中获取某些值,执行一些次要的格式化/转换步骤,并吐出一个逗号分隔的字符串。 Up until now, I've been just writing those strings straight into a new csv file.
到目前为止,我只是将这些字符串直接写入新的csv文件中。 However, now I'm trying to utilize the csv library to handle that more effectively.
但是,现在我正在尝试利用csv库更有效地处理该问题。
import csv
mydata = ["This, is, a, test, row",
"This is, another test, row, my, dude",
"100, 200, 300, 400, 500"]
with open('test.csv', 'w') as target:
writer = csv.writer(target, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(mydata)
Results in this: 结果:
"This, is, a, test, row","This is, another test, row, my, dude","100, 200, 300, 400, 500"
Not really what I'm looking for. 并不是我要找的东西。 Whereas
writer.writerows(mydata)
results in this: 而
writer.writerows(mydata)
导致以下结果:
T,h,i,s,",", ,i,s,",", ,a,",", ,t,e,s,t,",", ,r,o,w
T,h,i,s, ,i,s,",", ,a,n,o,t,h,e,r, ,t,e,s,t,",", ,r,o,w,",", ,m,y,",", ,d,u,d,e
1,0,0,",", ,2,0,0,",", ,3,0,0,",", ,4,0,0,",", ,5,0,0
Based on what I can tell, csv.writer()
is either iterating through a list of strings (the first example) or a string of characters (the second example). 根据我的判断,
csv.writer()
遍历字符串列表(第一个示例)或字符串(第二个示例)。 I've also tried iterating through each item in mydata
with a for loop, but that hasn't helped eiter. 我也尝试过使用for循环遍历
mydata
每个项目,但这并没有助长一臂之力。 For the life of me, I can't figure out the best way to use the writer to parse this data so that each item in my list corresponds to a row in my target csv file. 为了我的一生,我想不出使用编写器来解析此数据的最佳方法,这样列表中的每个项目都对应于目标csv文件中的一行。 Would it make sense to store the
mydata
data in a completely different data structure? 将
mydata
数据存储在完全不同的数据结构中是否有意义? What's the best way to handle this? 处理此问题的最佳方法是什么?
Make my data a two-dimensional list, and user writer.writerows() 将我的数据设为二维列表,然后使用用户writer.writerows()
import csv
mydata = [['This', 'is', 'a', 'test', 'row'],
['This', 'is', 'another', 'test', 'row', 'my', 'dude'],
['100', '200', '300', '400', '500']]
with open('test.csv', 'w') as target:
writer = csv.writer(target)
writer.writerows(mydata)
writerow() is best used for writing single lines, like headers: writerow()最适合用于编写单行,例如标头:
import csv
myheaders = ['Header1', 'Header2', 'Header3', 'Header4', 'Header5']
mydata = [['This', 'is', 'a', 'test', 'row'],
['This', 'is', 'another', 'test', 'row', 'my', 'dude'],
['100', '200', '300', '400', '500']]
with open('test.csv', 'w') as target:
writer = csv.writer(target)
writer.writerow(myheaders)
writer.writerows(mydata)
You need to use writerows
with a list of lists, like so: 您需要使用
writerows
与列表,像这样的列表:
import csv
mydata = [["This, is, a, test, row"],
["This is, another test, row, my, dude"],
["100, 200, 300, 400, 500"]]
with open('test.csv', 'w') as target:
writer = csv.writer(target, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerows(mydata)
Your current code is treating each string as a list of columns, hence the unexpected output. 您当前的代码将每个字符串视为列列表,因此会出现意外输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.