How to read a csv file with python
Posted
by john
on Stack Overflow
See other posts from Stack Overflow
or by john
Published on 2009-10-20T08:58:39Z
Indexed on
2010/04/27
8:43 UTC
Read the original article
Hit count: 454
Hello,
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()
© Stack Overflow or respective owner