首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python将csv导入列表

Python将csv导入列表
EN

Stack Overflow用户
提问于 2014-07-10 03:48:13
回答 11查看 685.2K关注 0票数 230

我有一个包含大约2000条记录的CSV文件。

每条记录都有一个字符串和一个类别:

代码语言:javascript
复制
This is the first line,Line1
This is the second line,Line2
This is the third line,Line3

我需要将这个文件读入如下所示的列表中:

代码语言:javascript
复制
data = [('This is the first line', 'Line1'),
        ('This is the second line', 'Line2'),
        ('This is the third line', 'Line3')]

如何使用Python将此CSV导入到我需要的列表中?

EN

回答 11

Stack Overflow用户

发布于 2016-11-24 21:52:27

Pandas非常擅长处理数据。下面是一个如何使用它的示例:

代码语言:javascript
复制
import pandas as pd

# Read the CSV into a pandas data frame (df)
#   With a df you can do many things
#   most important: visualize data with Seaborn
df = pd.read_csv('filename.csv', delimiter=',')

# Or export it in many ways, e.g. a list of tuples
tuples = [tuple(x) for x in df.values]

# or export it as a list of dicts
dicts = df.to_dict().values()

一个很大的优点是,pandas可以自动处理标题行。

如果你没有听说过Seaborn,我建议你去看看。

另请参阅:How do I read and write CSV files with Python?

熊猫#2

代码语言:javascript
复制
import pandas as pd

# Get data - reading the CSV file
import mpu.pd
df = mpu.pd.example_df()

# Convert
dicts = df.to_dict('records')

df的内容是:

代码语言:javascript
复制
     country   population population_time    EUR
0    Germany   82521653.0      2016-12-01   True
1     France   66991000.0      2017-01-01   True
2  Indonesia  255461700.0      2017-01-01  False
3    Ireland    4761865.0             NaT   True
4      Spain   46549045.0      2017-06-01   True
5    Vatican          NaN             NaT   True

dicts的内容是

代码语言:javascript
复制
[{'country': 'Germany', 'population': 82521653.0, 'population_time': Timestamp('2016-12-01 00:00:00'), 'EUR': True},
 {'country': 'France', 'population': 66991000.0, 'population_time': Timestamp('2017-01-01 00:00:00'), 'EUR': True},
 {'country': 'Indonesia', 'population': 255461700.0, 'population_time': Timestamp('2017-01-01 00:00:00'), 'EUR': False},
 {'country': 'Ireland', 'population': 4761865.0, 'population_time': NaT, 'EUR': True},
 {'country': 'Spain', 'population': 46549045.0, 'population_time': Timestamp('2017-06-01 00:00:00'), 'EUR': True},
 {'country': 'Vatican', 'population': nan, 'population_time': NaT, 'EUR': True}]

熊猫#3

代码语言:javascript
复制
import pandas as pd

# Get data - reading the CSV file
import mpu.pd
df = mpu.pd.example_df()

# Convert
lists = [[row[col] for col in df.columns] for row in df.to_dict('records')]

lists的内容是:

代码语言:javascript
复制
[['Germany', 82521653.0, Timestamp('2016-12-01 00:00:00'), True],
 ['France', 66991000.0, Timestamp('2017-01-01 00:00:00'), True],
 ['Indonesia', 255461700.0, Timestamp('2017-01-01 00:00:00'), False],
 ['Ireland', 4761865.0, NaT, True],
 ['Spain', 46549045.0, Timestamp('2017-06-01 00:00:00'), True],
 ['Vatican', nan, NaT, True]]
票数 49
EN

Stack Overflow用户

发布于 2018-01-05 11:12:18

Python3更新:

代码语言:javascript
复制
import csv
from pprint import pprint

with open('text.csv', newline='') as file:
    reader = csv.reader(file)
    res = list(map(tuple, reader))

pprint(res)

输出:

代码语言:javascript
复制
[('This is the first line', ' Line1'),
 ('This is the second line', ' Line2'),
 ('This is the third line', ' Line3')]

如果csvfile是文件对象,则应使用newline=''打开它。

csv module

票数 10
EN

Stack Overflow用户

发布于 2014-07-10 03:53:26

如果您确定输入中没有逗号,除了分隔类别之外,您可以在,上使用read the file line by linesplit,然后将结果推送到List

也就是说,看起来您正在查看CSV文件,因此您可以考虑使用the modules

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24662571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档