Skip to content Skip to sidebar Skip to footer

Parsing A Csv File With Column Data In Python

I want to read the first 3 columns of a csv file and do some modification before storing them. Data in csv file: {::[name]str1_str2_str3[0]},1,U0.00 - Sensor1 Not Ready\nTry Again,

Solution 1:

My suggestion is to read a whole line as a string, and then extract desired data with re module like this:

import re

term = '\[(\d)\].*,(\d+),.*-\s([\w\s]+)\\n'

line = '{::[name]str1_str2_str3[0]},1,U0.00 - Sensor1 Not Ready\nTry Again,1,0,12'
capture = list(re.search(term, line).groups())
capture[-1] = '_'.join(capture[-1].split()).upper()
result = ','.join(capture)
#0,1,Sensor1_Not_Ready

Post a Comment for "Parsing A Csv File With Column Data In Python"