使用逗号CSV Python分隔数据 [英] Separate data with a comma CSV Python

查看:706
本文介绍了使用逗号CSV Python分隔数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据需要写入CSV文件。数据如下

I have some data that needs to be written to a CSV file. The data is as follows

A        ,B    ,C
a1,a2    ,b1   ,c1
a2,a4    ,b3   ,ct

第一列中有逗号。整个数据在一个列表中,我想写入一个CSV文件,用逗号分隔,而不干扰列A中的数据。我该怎么做?提及 delimiter =','将它整体分成四列。

The first column has comma inside it. The entire data is in a list that I'd like to write to a CSV file, delimited by commas and without disturbing the data in column A. How can I do that? Mentioning delimiter = ',' splits it into four columns on the whole.

推荐答案

只需使用 csv.writer c> csv 模块。

Just use the csv.writer from the csv module.

import csv

data =  [['A','B','C']
         ['a1,a2','b1','c1']
         ['a2,a4','b3','ct']]

fname = "myfile.csv"    
with open(fname,'wb') as f:
    writer = csv.writer(f)
    for row in data:
        writer.writerow(row)

https://docs.python.org/2/library/csv.html#csv.writer

这篇关于使用逗号CSV Python分隔数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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