onkar
onkar

Reputation: 4547

Parsing a csv file in python django

I am trying to read data from a csv file that has been uploaded. First, I am getting each row then trying to read data from each row by splitting it with comma, which is good for ideal case but if contains "," like address field, it will parse data in wrong format.

I would like to have a more reliable solution for val = v.split(',')

My code is

 upload_file = request.FILES['upload_file']
    data = [row for row in csv.reader(upload_file.read().splitlines())]

    for v in data:
       # v is every row
       val = v.split(',') #spliting value of every row to get each record of every row 

Upvotes: 4

Views: 16357

Answers (4)

alacy
alacy

Reputation: 5064

If you read in the file using a simple read statement like:

data = upload_file.read()

# you can use the re library --> import re
rows = re.split('\n', data) # splits along new line
for index, row in enumerate(rows):
    cells = row.split(',')
    # do whatever you want with the cells in each row
    # this also keeps an index if you want the cell's row index

Or you can use the csv.reader module:

file_reader = csv.reader(upload_file, delimiter=',')
for row in file_reader:
    # do something with row data.
    print(row)
    # would print the rows like
    # I, like, to, ride, my, bicycle
    # I, like, to, ride, my, bike

Upvotes: 7

sharshofski
sharshofski

Reputation: 147

You could use pandas. Here's an example based on this question:

>>> import sys, pandas
>>> if sys.version_info[0] < 3:
    from StringIO import StringIO
else:
    from io import StringIO
## I assume your input is something like this:
>>> string = "a,b,c\n1,2,3\n4,5,6\n7,8,9\n"
>>> stringIO = StringIO(string)
>>> df = pandas.DataFrame.from_csv(stringIO, sep=',', index_col=False)
>>> print df
   a  b  c
0  1  2  3
1  4  5  6
2  7  8  9

>>> print df.columns
Index([u'a', u'b', u'c'], dtype='object')

## access elements
>>> print df['a'][3]
7

Documentation for DataFrame.from_csv

Upvotes: -1

Emilien
Emilien

Reputation: 2445

CSV means comma separated values. If you need to encode a string in a CSV, you usually surround it with quotes. Otherwise you won't be able to parse correctly the file:

$ cat sample.csv 
"131, v17",foo
bar,qux


>>> import csv
>>> with open('sample.csv', 'rb') as f:
...   r = csv.reader(f)
...   for row in r:
...     print row
... 
['131, v17', 'foo']
['bar', 'qux']

And of course if you ommit the quotes, the first line is parsed into 3 fields.

Upvotes: 0

nitimalh
nitimalh

Reputation: 961

If you're looking to split and get access to the words in each line then re.split would be a good option :

re.split('\W+', 'Words, words, words.')
['Words', 'words', 'words', '']

Sample code from : https://docs.python.org/2/library/re.html

Upvotes: 0

Related Questions