Search code examples
pythonpython-3.xcsvexport-to-csvopencsv

How to extract specific values from imported csv and write it?


I have a csv in the below format:

number,values
686790635,{'2019-05-24T13:46:35': 'CSCvp87661'}
686235835,{'2019-02-27T14:13:53': 'CSCvj48931'}
689324672,{'2020-06-19T08:50:53': 'CSCvs42803'}
689995407,{'2020-09-27T05:51:39': 'CSCvg55782'}
688751767,{'2020-03-26T11:28:44': 'CSCvc81396'}
689868626,"{'2020-09-10T01:29:51': 'CSCux76799', '2020-09-10T01:29:53': 'CSCux76799'}"
689206940,{'2020-06-02T20:40:44': 'CSCvo65492'}
686259208,"{'2019-03-11T02:55:43': 'CSCvi66732', '2019-03-11T02:55:52': 'CSCvg81628'}"
689030956,{'2020-05-07T10:05:09': 'CSCvh19223'}

Here I was trying to extract the values as in below list:

values = [CSCvp87661,CSCvj48931, CSCvs42803, CSCvg55782, CSCvc81396, CSCux76799, CSCux76799, CSCvo65492, CSCvi66732, CSCvg81628, CSCvh19223]

I was trying to loop over and iterate the values but not able to get it in the exact list format. Any help would be helpful.


Solution

  • First of all - these is odd csv format. There are better options to structure these data - e.g. JSON

    import csv
    from ast import literal_eval
    from itertools import chain
    with open('data.csv') as f:
        rdr = csv.DictReader(f)
        data = list(chain(*(literal_eval(line['values']).values() for line in rdr)))
    print(data)
    

    output

    ['CSCvp87661', 'CSCvj48931', 'CSCvs42803', 'CSCvg55782', 'CSCvc81396', 'CSCux76799', 'CSCux76799', 'CSCvo65492', 'CSCvi66732', 'CSCvg81628', 'CSCvh19223']