how to split .csv data in python? -
when split csv file using method:
with open(fname) f: line in f: = line.strip().split()
i expected output is:
['chicago', 'white', 'sox,"valentin,', 'jose","5,000,000",outfielder,,,,'] ['detroit', 'tigers,"bernero,', 'adam","314,000",pitcher,,,,']
and on...
how split data right pieces (team, player, salary, position)?
data set (in xls) here:
american league baseball salaries (2003) team player salary position new york yankees acevedo, juan 9,00,000 pitcher new york yankees anderson, jason 3,00,000 pitcher new york yankees clemens, roger 1,01,00,000 pitcher new york yankees contreras, jose 55,00,000 pitcher
you can use zip
function columns of file , use csv
module reading csv
file :
import csv open('file_.csv','rb') f : csvreader=csv.reader(f,delimiter=' ') print zip(*csvreader)
and huge files use itertools.izip
:
import csv itertools import izip open('file_.csv','rb') f : csvreader=csv.reader(f,delimiter=' ') print list(izip(*csvreader))
as izip
returns generator if want loop on don't need list
(its printing content)
also note need use correct delimiter used space
instance,you can change use correct delimiter!
also can put result in dictionary :
import csv itertools import izip open('file_.csv','rb') f : csvreader=csv.reader(f,delimiter='\t') keys=next(csvreader) a=izip(*csvreader) d=dict(zip(keys,a)) print d print d['salary']
result :
{'salary': ('9,00,000', '3,00,000', '1,01,00,000', '55,00,000'), 'player': ('acevedo, juan', 'anderson, jason', 'clemens, roger', 'contreras, jose'), 'position': ('pitcher', 'pitcher', 'pitcher', 'pitcher'), 'team': ('new york yankees', 'new york yankees', 'new york yankees', 'new york yankees')} ('9,00,000', '3,00,000', '1,01,00,000', '55,00,000')
Comments
Post a Comment