提问者:小点点

如何使用Python读取和写入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是否使用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)]

如何使用Python将数据写入CSV文件?


共3个答案

匿名用户

这里有一些最小的完整示例如何读取CSV文件以及如何使用Python编写CSV文件。

纯Python

import csv

# 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
with open("test.csv", "wt") as fp:
    writer = csv.writer(fp, delimiter=",")
    # writer.writerow(["your", "header", "foo"])  # write header
    writer.writerows(data)

# Read CSV file
with open("test.csv") as fp:
    reader = csv.reader(fp, delimiter=",", quotechar='"')
    # next(reader, None)  # skip the headers
    data_read = [row for row in reader]

print(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']]

请注意,CSV只读取字符串。您需要手动转换为列类型。

之前有一个Python23版本(链接),但是Python2支持被放弃了。删除Python2内容大大简化了这个答案。

  • 如何将数据作为字符串(而不是文件)写入csv格式
  • 如何使用io。带csv模块的StringIO():如果您希望使用Flask动态提供CSV,而不实际将CSV存储在服务器上,那么这很有趣

看看我的实用程序包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文档。请注意,熊猫会自动推断是否有标题行,但您也可以手动设置。

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

其他一些库支持读取CSV文件,例如:

  • dask.dataframe.read_csv
  • spark.read.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文件读取到元组/迪克斯列表或熊猫数据帧后,它只是使用这种数据。没有特定的CSV。

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

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

  • 其他编程语言的支持
  • 阅读/写作表现
  • 紧凑度(文件大小)

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

如果你正在寻找一种制作配置文件的方法,你可能想阅读我的一篇简短的文章Python配置文件

匿名用户

首先,您需要导入csv

对于eg:

import csv

with open('eggs.csv', 'wb') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ',
                        quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

匿名用户

如果需要-在不使用csv模块的情况下读取csv文件:

rows = []
with open('test.csv') as f:
    for line in f:
        # strip whitespace
        line = line.strip()
        # separate the columns
        line = line.split(',')
        # save the line for use later
        rows.append(line)