当前位置:  开发笔记 > 编程语言 > 正文

如何使用Python读写CSV文件?

如何解决《如何使用Python读写CSV文件?》经验,为你挑选了1个好方法。

我有一个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

我如何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)]

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



1> Martin Thoma..:

以下是一些最简单的完整示例,介绍如何读取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)

之后,内容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']]

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)

有关

如何将数据写入csv格式为字符串(而不是文件)?

如何在csv模块中使用io.StringIO()?:如果您想要使用Flask即时提供CSV,而不实际将CSV存储在服务器上,这很有趣.

MPU

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

import mpu.io
data = mpu.io.read('example.csv', delimiter=',', quotechar='"', skiprows=None)
mpu.io.write('example.csv', data)

熊猫

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()

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

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

其他

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

dask.dataframe.read_csv

spark.read.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

常见文件结尾

.csv

使用数据

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

备择方案

JSON:很适合编写人类可读的数据; 非常常用(读写)

CSV:超简单格式(读写)

YAML:很高兴阅读,类似于JSON(读写)

pickle:Python序列化格式(读写)

MessagePack(Python包):更紧凑的表示(读写)

HDF5(Python包):很适合矩阵(读写)

XML:存在太多*叹*(读与写)

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

其他编程语言的支持

读/写性能

紧凑性(文件大小)

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

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

推荐阅读
TXCWB_523
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有