abn
abn

Reputation: 1373

Get header:row_data from CSV file python

I have a csv file like below

h1,h2,h3,h4
a,b,,d
1,2,3,4
a1,,h5,jj

I'd like to get a list like this: For example, for 'a', I need h1:a,h2:b,h4:d. I could get headers and row data separately, however, I'm unable to concatenate them in the desired way. Also, I don't want blanks to be printed as 'nan'

Upvotes: 0

Views: 1685

Answers (3)

chfw
chfw

Reputation: 4592

You also can use my wrapper library over csv module to do it:

>>> import pyexcel as pe
>>> s=pe.load("example.csv", name_columns_by_row=0)
>>> records = s.to_records()
>>> records
[{'h2': u'b', 'h3': u'', 'h1': u'a', 'h4': u'd'}, {'h2': u'2', 'h3': u'3', 'h1': u'1', 'h4': u'4'}, {'h2': u'', 'h3': u'h5', 'h1': u'a1', 'h4': u'jj'}]
>>> s.column['h1']
[u'a', u'1', u'a1']
>>> zip(s.column['h1'], records)
[(u'a', {'h2': u'b', 'h3': u'', 'h1': u'a', 'h4': u'd'}), (u'1', {'h2': u'2', 'h3': u'3', 'h1': u'1', 'h4': u'4'}), (u'a1', {'h2': u'', 'h3': u'h5', 'h1': u'a1', 'h4': u'jj'})]

More documentation can be found here

Upvotes: 1

rmarques
rmarques

Reputation: 91

You can easily do this with the csv module and dict comprehensions:

import csv

with open('test.csv', 'r') as f:                                                                                                                                  
        reader = csv.reader(f)                                                                                                                                        
        result = []                                                                                                                                                   
        header = reader.next()                                                                                                                                        
        for row in reader:                                                                                                                                            
            result.append({k: v for k, v in zip(header, row) if v != ''}) 

Upvotes: 1

reptilicus
reptilicus

Reputation: 10397

Something like this might work

import numpy as np
import pandas
df = pandas.read_csv('some_file')
for row in df.to_dict('records'):
   print {k:v for k,v in row.iteritems() if v is not np.nan}

Upvotes: 1

Related Questions