Breme Arunprasath
Breme Arunprasath

Reputation: 13

How to parse a CSV file if the column I want to Parse is in the middle of the CSV file in Python?

I have a CSV Data as below,

Schedule Start Time Schedule End Time   Interval    System IP - Login User
3-30-2018 1:22  03-30-2018 02:21    00 hour 5 minute    10.1.1.3


Grep Command Result         

Schedule Time   ECM MAC ImageName   
03-30-2018 01:22    B4:A5:EF:F2:27  UNAVAILABLE 
03-30-2018 01:27    B4:A5:EF:F2:27  UNAVAILABLE 
03-30-2018 01:32    B4:A5:EF:F2:27  UNAVAILABLE 
03-30-2018 01:37    B4:A5:EF:F2:27  UNAVAILABLE 
03-30-2018 01:42    B4:A5:EF:F2:27  UNAVAILABLE 
03-30-2018 01:47    B4:A5:EF:F2:27  UNAVAILABLE 
03-30-2018 01:52    B4:A5:EF:F2:27  UNAVAILABLE 
03-30-2018 01:57    B4:A5:EF:F2:27  UNAVAILABLE 
03-30-2018 02:02    B4:A5:EF:F2:27  UNAVAILABLE 
03-30-2018 02:07    B4:A5:EF:F2:27  UNAVAILABLE 
03-30-2018 02:12    B4:A5:EF:F2:27  UNAVAILABLE 
03-30-2018 02:17    B4:A5:EF:F2:27  UNAVAILABLE 

I want to parse Schedule Time,ECM MAC,ImageName these columns , How can I do it in python, I Tried

file1='testcsv.csv'
red=pd.read_csv(file1,skipinitialspace=True,usecols=['Schedule Time','ECM MAC','ImageName'])
print red

But I'm getting,

  File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 1740, in __init__
    raise ValueError("Usecols do not match names")

Can Anyone suggest the best solution?Thanks

Upvotes: 0

Views: 145

Answers (1)

Juan Osorio
Juan Osorio

Reputation: 380

I am not sure you can parse that easily with read_csv. But you can just skip the first rows and get to the point the information is located:

pd.read_csv('schedule.csv',skiprows=8, sep=' ', 
            index_col=False, skipinitialspace=True,  
            names=['Schedule Time', 'ECM', 'MAC', 'ImageName'])`

This is using "names" for the colum names. It is setting the separator to be ' ', as the default is ',' and is skiping the first rows of information. The index_col=False force read_csv to not using the first column as the index column.

Upvotes: 1

Related Questions