CSV IO python:将csv文件转换为列表列表 [英] CSV IO python: converting a csv file into a list of lists

查看:973
本文介绍了CSV IO python:将csv文件转换为列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将csv文件转换为列表列表,其中每一行都是具有较大列表的条目列表?

How to convert a csv file into a list of lists where each line is a list of entries with in a bigger list?

我遇到了麻烦,因为我的某些条目中有逗号

I'm having trouble with this because some of my entries have a comma in them

一个文件,如:

'1','2','3'  
'1,1','2,2','3,3'  

成为:

[['1','2','3']['1','1','2','2','3','3']]

代替:

[['1','2','3']['1,1','2,2','3,3']

推荐答案

in your data '2,2' is one string. But csv reader can deal with this:
import csv
result = []
with open("data", 'r') as f:
        r = csv.reader(f, delimiter=',')
        # next(r, None)  # skip the headers
        for row in r:
            result.append(row)

print(result)

[["'1'", "'2'", "'3'"], ["'1", "1'", "'2", "2'", "'3", '3']]

这篇关于CSV IO python:将csv文件转换为列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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