我有一个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
# -*- 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,则必须安装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,以获得一个超级简单易记的实用程序:
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文件由许多其他库支持,例如:
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中的配置文件
| 归档时间: |
|
| 查看次数: |
37998 次 |
| 最近记录: |