mardi 23 février 2021

Python if-condition import json file base on different column values

I'd like to import different JSON format file based on a csv's column('type' column) values, JSON file is used to parse data from csv. Finally the csv would be transformed to JSON format.

Different type needs different JSON format file, so I'd like to import several JSON file to do the paring.

Please find the csv format:

type date name  ...
aaa   1   Jay
bbb   2   Jenny
ccc   3   Lucy
...

And codes:

if __name__ == "__main__":
    fp = './data.csv'
    if fp[fp['type'] == 'aaa'] or fp[fp['type'] == 'bbb']:
       format_fp = './type1_format.json'
    elif fp[fp['type'] == 'ccc']:
        format_fp = './type3_format.json'

but error: TypeError: string indices must be integers

Question: How could I import and use different json format file based on different type?

Many thanks!

Aucun commentaire:

Enregistrer un commentaire