Reputation: 51
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.
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()
Upvotes: 5
Views: 16842
Reputation: 8940
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.
>>> 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:
>>> import pandas as pd
>>> pd.read_csv(csvfile)
This has the advantage of creating pandas.DataFrame, which is a better structure for data analysis.
Upvotes: 7
Reputation: 101
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.
Upvotes: 2
Reputation: 599450
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.
Upvotes: 0