如何使用Python读写CSV文件?

Mar*_*oma 33 python csv

我有一个example.csv包含内容的文件

1,"A towel,",1.0
42," it says, ",2.0
1337,is about the most ,-1
0,massively useful thing ,123
-2,an interstellar hitchhiker can have.,3
Run Code Online (Sandbox Code Playgroud)

我如何example.csv用Python 阅读?

同样,如果我有

data = [(1, "A towel,", 1.0),
        (42, " it says, ", 2.0),
        (1337, "is about the most ", -1),
        (0, "massively useful thing ", 123),
        (-2, "an interstellar hitchhiker can have.", 3)]
Run Code Online (Sandbox Code Playgroud)

如何data使用Python 写入CSV文件?

Mar*_*oma 40

以下是一些最简单的完整示例,介绍如何读取CSV文件以及如何使用Python编写CSV文件.

Python 2 + 3:读取CSV文件

纯Python

# -*- coding: utf-8 -*-

import csv
import sys

# Define data
data = [(1, "A towel,", 1.0),
        (42, " it says, ", 2.0),
        (1337, "is about the most ", -1),
        (0, "massively useful thing ", 123),
        (-2, "an interstellar hitchhiker can have.", 3)]

# Write CSV file
kwargs = {'newline': ''}
mode = 'w'
if sys.version_info < (3, 0):
    kwargs.pop('newline', None)
    mode = 'wb'

with open('test.csv', mode, **kwargs) as fp:
    writer = csv.writer(fp, delimiter=',')
    # writer.writerow(["your", "header", "foo"])  # write header
    writer.writerows(data)

# Read CSV file
kwargs = {'newline': ''}
mode = 'r'
if sys.version_info < (3, 0):
    kwargs.pop('newline', None)
    mode = 'rb'
with open('test.csv', mode, **kwargs) as fp:
    reader = csv.reader(fp, delimiter=',', quotechar='"')
    # next(reader, None)  # skip the headers
    data_read = [row for row in reader]

print(data_read)
Run Code Online (Sandbox Code Playgroud)

之后,内容data_read

[['1', 'A towel,', '1.0'],
 ['42', ' it says, ', '2.0'],
 ['1337', 'is about the most ', '-1'],
 ['0', 'massively useful thing ', '123'],
 ['-2', 'an interstellar hitchhiker can have.', '3']]
Run Code Online (Sandbox Code Playgroud)

Unicode和Python 2.X

如果要编写Unicode,则必须安装unicodecsv.千万不能用打开文件codecs.open,而只是用open.写下来

import unicodecsv as csv
# Write CSV file
with open('test.csv', 'w', newline='') as fp:
    writer = csv.writer(fp, encoding='utf-8')
    # writer.writerow(["your", "header", "foo"])  # write header
    writer.writerows(data)
Run Code Online (Sandbox Code Playgroud)

有关

MPU

看看我的实用程序包mpu,以获得一个超级简单易记的实用程序:

import mpu.io
data = mpu.io.read('example.csv', delimiter=',', quotechar='"', skiprows=None)
mpu.io.write('example.csv', data)
Run Code Online (Sandbox Code Playgroud)

熊猫

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('myfile.csv', sep=',')
print(df)

# 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()
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅read_csv文档.请注意,如果有标题行,pandas会自动推断,但您也可以手动设置它.

如果你还没有听说过Seaborn,我建议看一下.

其他

读取CSV文件由许多其他库支持,例如:

创建了CSV文件

1,"A towel,",1.0
42," it says, ",2.0
1337,is about the most ,-1
0,massively useful thing ,123
-2,an interstellar hitchhiker can have.,3
Run Code Online (Sandbox Code Playgroud)

常见文件结尾

.csv

使用数据

在将CSV文件读取到元组/ dicts列表或Pandas数据帧之后,它只是处理这种数据.没有特定的CSV.

备择方案

对于您的应用程序,以下可能很重要:

  • 其他编程语言的支持
  • 读/写性能
  • 紧凑性(文件大小)

另请参见:数据序列化格式的比较

如果您正在寻找一种制作配置文件的方法,您可能希望阅读我的简短文章Python中的配置文件

  • @icedwater 这是一种可能性。但是,我更喜欢 Pandas:(1)它自动处理标头(2)它直接从路径加载文件并且不需要文件指针(3)它有更好的“导出”选项(如 dict 导出 - 是的,您也可以使用 CSV 做到这一点。但 Pandas 更简单)。但请随意发布不需要 Pandas 的解决方案:-) (2认同)
  • @icedwater好的,我添加了一个纯粹的“csv”解决方案(现在它的结构也与我对其他文件格式(如 YAML 和 JSON)的其他答案一致) (2认同)

归档时间:

查看次数:

37998 次

最近记录:

5 年,12 月 前