Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a csv key value file using python

Tags:

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

like image 752
user2479840 Avatar asked Sep 06 '25 23:09

user2479840


2 Answers

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.

like image 135
Pedro Werneck Avatar answered Sep 09 '25 04:09

Pedro Werneck


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.

like image 32
jgranger Avatar answered Sep 09 '25 03:09

jgranger