Asked 7 years ago
23 Mar 2017
Views 1213
Phpworker

Phpworker posted

how to parse csv in PYTHON ?

csv content like this

b'Date,Open,High,Low,Close,Volume,Adj Close\n2017-02-13,18.75,19.10,17.90,18.05,21563900,18.05\n2017-02-10,17.30,17.65,17.05,17.20,7616200,17.20\n


trying to parse csv in Python ?

to read file in Python

fp=open('suzlon.csv')
d=fp.read()


than how can i explode string with new line and comma . is there explode like PHP do in Python ?
QuickIos

QuickIos
answered Aug 9 '21 00:00

Use Pandas to read CSV if you are working with predication, Pandas have many other good features which good to handle data


import pandas as pd
df = pd.read_csv('abc.csv')
print(df)


just two lines of code read CSV and give very easy data structure in return
ravi

ravi
answered Nov 30 '-1 00:00


fp=open('suzlon.csv')
d=fp.read()
print(d)
j=d.split("\\n")
for l in j:
    p=l.split(",")
    print(p)


use split function to split with "\\n" new line and than with comma in Python

last output will be array

answered Nov 30 '-1 00:00

there is no explode function in Python as PHP do , but you can use Split

use splitline to split line in Python
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

Import CSV module for Python
it a fast CSV reading module for Python
http://www.object-craft.com.au/projects/csv/


import csv
filename = 'suzlon.csv'
csv_file = csv.DictReader(open(filename, 'rb'), delimiter=',', quotechar='"')
Post Answer