简体   繁体   English

将逗号分隔值的字符串列表写入CSV

[英]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.

相关问题 在没有任何包的情况下将逗号分隔的字符串写入 csv 中的单个单元格 - writing comma separated strings to single cell in csv without any packages 将字符串列表写入csv文件时获取逗号分隔的字符 - Getting comma seperated chars when writing list of strings to a csv file 将字符串列表写入 csv - Writing a list of strings into a csv 将列表转换为 sql IN 运算符的逗号分隔字符串 - Convert a list to comma separated strings for sql IN operator Python字符串列表到1个字符串,用逗号和引号(,&“)分隔 - Python list of strings to 1 string separated by comma & quotes (, & ") 如何使用逗号分隔的字符串制作列表? - How to make a list with a comma separated strings? 在 elasticsearch 上搜索逗号分隔的字符串列表 - Search a list of comma separated strings on elasticsearch 在列表或字符串中获取字典值的更好方法。 每个值都是一个字符串,由多个用逗号分隔的字符串组成 - Better way to get dictionary values in a list or a sting. Each value is a string consisting of multiple strings separated by a comma PySpark列表中有条件分割逗号分隔的值 - Conditionally split comma separated values in PySpark list 将列值拆分为以逗号分隔的值列表 - Splitting a column value into list of values separated by comma
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM