[英]Parsing a csv file in python django
我正在嘗試從已上傳的csv文件讀取數據。 首先,我要獲取每一行,然后嘗試通過用逗號分割數據來讀取每一行中的數據,這對於理想情況是很好的,但是如果包含“,”(如地址字段),它將以錯誤的格式解析數據。
我想為val = v.split(',')
提供一個更可靠的解決方案
我的代碼是
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
如果您使用簡單的read語句讀取文件,例如:
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
或者,您可以使用csv.reader
模塊:
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
如果您希望拆分並訪問每一行中的單詞,那么re.split
將是一個不錯的選擇:
re.split('\W+', 'Words, words, words.')
['Words', 'words', 'words', '']
來自的示例代碼: https : //docs.python.org/2/library/re.html
CSV表示逗號分隔的值。 如果需要在CSV中編碼字符串,則通常用引號將其引起來。 否則,您將無法正確解析文件:
$ 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']
當然,如果您省略引號,則第一行將解析為3個字段。
>>> 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.