csv:writer.writerows()分割我的字符串输入 [英] csv: writer.writerows() splitting my string inputs

查看:696
本文介绍了csv:writer.writerows()分割我的字符串输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要写入csv文件的字符串列表.列表list_results看起来像

I have a list of strings which I would like to write to a csv file. The list list_results looks like

['False, 60, 40 ', 'True, 70, 30, ']

所以,我会尝试这样的事情:

So, I would try something like this:

with open('example1.csv', 'w') as result:
    writer = csv.writer(result, delimiter=",")
    writer.writerow( ('Correct?', 'Successes', 'Failures') )
    writer.writerows(list_results)

不幸的是,数据没有写入三列.我发现:

Unfortunately, the data doesn't write into three columns. I find:

['Correct?', 'Successes', 'Failures']  
['F', 'a', 'l', 's', 'e', ',', ' ', '6', '0', ',', ' ', '4', '0', ' ']  
['T', 'r', 'u', 'e', ',', ' ', '7', '0', ',', ' ', '3', '0', ',', ' ']

如何正确格式化此行为以获取

How do I format this behavior appropriately to get

['Correct?', 'Successes', 'Failures']  
['False', 60, 40]  
['True', 70, 30]

最好采用%s %d %d格式?

推荐答案

您正在将writer.writerows()与结尾处的s一起使用.该方法需要一个列表列表,但是您传入了一个字符串列表. writerows()方法本质上是这样做的:

You are using writer.writerows() with an s at the end. That method expects a list of lists, but you passed in a list of strings. The writerows() method essentially does this:

def writerows(self, rows):
    for row in rows:
        self.writerow(row)

,其中每一行必须是一列列.字符串是单个字符的序列,因此这就是您所写的:单个字符由您选择的定界符分隔.

where each row must be a sequence of columns. A string is a sequence of individual characters, so that's what you get written: individual characters separated by your chosen delimiter.

您需要将字符串分成几列,自己不要包含逗号,包括以下内容是writer对象的工作:

You'll need to split out your string into columns, don't include the commas yourself, it's the job of the writer object to include those:

with open('example1.csv', 'w') as result:
    writer = csv.writer(result, delimiter=",")
    writer.writerow(('Correct?', 'Successes', 'Failures'))
    for row in list_results:
        columns = [c.strip() for c in row.strip(', ').split(',')]
        writer.writerow(columns)

或使用生成器表达式,以便您可以继续使用writerows():

or using a generator expression so you can keep using writerows():

with open('example1.csv', 'w') as result:
    writer = csv.writer(result, delimiter=",")
    writer.writerow(('Correct?', 'Successes', 'Failures'))
    writer.writerows([c.strip() for c in r.strip(', ').split(',')]
                     for r in list_results)

演示:

>>> import csv
>>> list_results = ['False, 60, 40 ', 'True, 70, 30, ']
>>> import csv
>>> import sys
>>> list_results = ['False, 60, 40 ', 'True, 70, 30, ']
>>> writer = csv.writer(sys.stdout)
>>> writer.writerow(('Correct?', 'Successes', 'Failures'))
Correct?,Successes,Failures
>>> for row in list_results:
...     columns = [c.strip() for c in row.strip(', ').split(',')]
...     writer.writerow(columns)
...
False,60,40
True,70,30
>>> writer.writerows([c.strip() for c in r.strip(', ').split(',')]
...                  for r in list_results)
False,60,40
True,70,30

这篇关于csv:writer.writerows()分割我的字符串输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆