user2479840
user2479840

Reputation: 527

How to parse a csv key value file using python

I have a requirement to parse a key value coma separated file.Please find attached the sample log file format .

event_1,log_time:2013-11-05T08:33:37:293+00,user_id:2535285332077170,profile_id:8,nickname:2535285332077170,rank_id:7,shop_tr_status:OK,
event_2,log_time:2013-11-05T08:33:37:344+00,rule_id:18372990742769963554,user_id:2535285332077170,profile_id:8,
event_3,log_time:2013-11-05T08:33:37:401+00,user_id:2535285332077170,profile_id:8,nickname:2535285332077170

My requirement is if it is event 1 then I need log_time and nickname

if it is event_2 then I need userid and profileid

if it is event_3 then i need userid and nickname

Can anyone suggest what is the best way to proceed with this

Upvotes: 4

Views: 4900

Answers (3)

Pedro Werneck
Pedro Werneck

Reputation: 41898

Forget csv.reader. You should use tablib with dynamic columns:

ds = tablib.Dataset()
ds.csv = open(csvfile).read()

Then you can add the columns with:

def event_data(row):
    if row[0] == 'event_1':
        return [row[1], row[4]]
    # .. and so forth

ds.append_col(event_data, header='Event data')

And getting that column should give you the data you want, according to the first column.

Upvotes: 3

jgranger
jgranger

Reputation: 264

If you have headers and want to pull out specific column data try

import csv
with open('csv_file.csv', 'rU') as csv_file:
        csvreader = csv.DictReader(csv_file)
        for row in csvreader:
            print("Rows: " + str(row))
            if row['header1'] == '1':
                print('Data: ' + row['header2'])

CSV File contents:

header1,header2,header3
1,2,3
4,5,6

Output:

Rows: {'header2': '2', 'header3': '3', 'header1': '1'}
Data: 2
Rows: {'header2': '5', 'header3': '6', 'header1': '4'}

If you have a lot of data adding headers can make your life easier or you will have to use csv.reader() and parse the contents and split as needed.

Upvotes: 1

Ramchandra Apte
Ramchandra Apte

Reputation: 4079

Use for row incsv.reader(), then place an if and some elifs in the for loop.

Upvotes: 0

Related Questions