python 如何用python读取一个csv文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1593318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read a csv file with python
提问by john
I'm trying to read a csv file but it doesn't work. I can read my csv file but when I see what I read, there where white space between values.
我正在尝试读取 csv 文件,但它不起作用。我可以读取我的 csv 文件,但是当我看到我读取的内容时,值之间有空格。
Here is my code
这是我的代码
# -*- coding: iso-8859-1 -*-
import sql_db, tmpl_macros, os
import security, form, common
import csv
class windows_dialect(csv.Dialect):
"""Describe the usual properties of unix-generated CSV files."""
delimiter = ','
quotechar = '"'
doublequote = 1
skipinitialspace = 0
lineterminator = 'n'
quoting = csv.QUOTE_MINIMAL
def reco(d):
cars = {210:'"', 211:'"', 213:"'", 136:'à', 143:'è', 142:'é'}
for c in cars:
d = d.replace(chr(c),cars[c])
return d
def page_process(ctx):
if ctx.req_equals('catalog_send'):
if 'catalog_file' in ctx.locals.__dict__:
contenu = ctx.locals.catalog_file[0].file.read()
#contenu.encode('')
p = csv.reader(contenu, delimiter=',')
inserted = 0
modified = 0
(cr,db) = sql_db.cursor_get()
for line in p:
if line:
logfile = open('/tmp/test.log', 'a')
logfile.write(line[0])
logfile.write('\n')
logfile.write('-----------------------------\n')
logfile.close()
回答by dalloliogm
I prefer to use numpy's genfromtxt rather than the standard csv library, because it generates numpy's recarray, which are clean data structures to store data in a table-like object.
我更喜欢使用 numpy 的 genfromtxt 而不是标准的 csv 库,因为它生成 numpy 的 recarray,这是一种干净的数据结构,可以将数据存储在一个类似表的对象中。
>>> from numpy import genfromtxt
>>> data = genfromtxt(csvfile, delimiter=',', dtype=None)
# data is a table-like structure (a numpy recarray) in which you can access columns and rows easily
>>> data['firstcolumn']
<content of the first column>
EDIT: This answer is quite old. While numpy.genfromtxt, nowadays most people would use pandas:
编辑:这个答案已经很老了。虽然 numpy.genfromtxt,但现在大多数人会使用 Pandas:
>>> import pandas as pd
>>> pd.read_csv(csvfile)
This has the advantage of creating pandas.DataFrame, which is a better structure for data analysis.
这样做的好处是创建了pandas.DataFrame,这是一种更好的数据分析结构。
回答by Mark McEahern
If you have control over the data, use tab-delimited instead::
如果您可以控制数据,请改用制表符分隔:
import csv
import string
writer = open('junk.txt', 'wb')
for x in range(10):
writer.write('\t'.join(string.letters[:5]))
writer.write('\r\n')
writer.close()
reader = csv.reader(open('junk.txt', 'r'), dialect='excel-tab')
for line in reader:
print line
This produces expected results.
这会产生预期的结果。
A tip for getting more useful feedback: Demonstrate your problem through self-contained and complete example code that doesn't contain extraneous and unimportant artifacts.
获得更多有用反馈的提示:通过不包含无关和不重要的工件的自包含和完整的示例代码演示您的问题。
回答by Daniel Roseman
You don't do anything with the dialect you've defined. Did you mean to do this:
你不会对你定义的方言做任何事情。你是不是想这样做:
csv.register_dialect('windows_dialect', windows_dialect)
p = csv.reader(contenu, dialect='windows_dialect')
Also not sure what the reco
function is for.
也不确定这个reco
功能是做什么用的。