tech:script_python_conversion_json_vers_csv
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| tech:script_python_conversion_json_vers_csv [2026/11/06 08:14] – créée Jean-Baptiste | tech:script_python_conversion_json_vers_csv [2026/11/11 15:45] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 9: | Ligne 9: | ||
| ~~~python | ~~~python | ||
| #! / | #! / | ||
| - | + | ||
| import sys | import sys | ||
| import json | import json | ||
| import argparse | import argparse | ||
| - | + | ||
| - | parser = argparse.ArgumentParser( | + | parser = argparse.ArgumentParser(epilog=""" |
| - | | + | |
| parser.add_argument(" | parser.add_argument(" | ||
| - | parser.add_argument(" | + | parser.add_argument(" |
| args = parser.parse_args() | args = parser.parse_args() | ||
| - | + | ||
| def concat(a, b): | def concat(a, b): | ||
| - | a=str(a) | + | a = str(a) |
| - | b=str(b) | + | b = str(b) |
| - | return a+ " | + | return a + " |
| def quote_string(thing): | def quote_string(thing): | ||
| Ligne 31: | Ligne 32: | ||
| else: | else: | ||
| return thing | return thing | ||
| + | |||
| def recursiv_if_json_dict(k, | def recursiv_if_json_dict(k, | ||
| Ligne 36: | Ligne 38: | ||
| for k2, v2 in v.items(): | for k2, v2 in v.items(): | ||
| recursiv_if_json_dict(concat(k, | recursiv_if_json_dict(concat(k, | ||
| - | else: | + | else: |
| write(k, v) | write(k, v) | ||
| + | |||
| def recursiv_if_json_list(thing): | def recursiv_if_json_list(thing): | ||
| Ligne 48: | Ligne 51: | ||
| new_list.append(elm) | new_list.append(elm) | ||
| return new_list | return new_list | ||
| - | + | ||
| def convert_json2csv(): | def convert_json2csv(): | ||
| with open(args.i, | with open(args.i, | ||
| json_load = json.load(json_file) | json_load = json.load(json_file) | ||
| - | + | ||
| if isinstance(json_load, | if isinstance(json_load, | ||
| for k, v in json_load.items(): | for k, v in json_load.items(): | ||
| Ligne 59: | Ligne 63: | ||
| for elm in json_load: | for elm in json_load: | ||
| recursiv_if_json_list | recursiv_if_json_list | ||
| + | |||
| def write(k, v): | def write(k, v): | ||
| with open(args.o, | with open(args.o, | ||
| - | print(quote_string(k), | + | print(quote_string(k), |
| def truncate(): | def truncate(): | ||
| with open(args.o, | with open(args.o, | ||
| csv_file.truncate(0) | csv_file.truncate(0) | ||
| + | |||
| def main(): | def main(): | ||
| Ligne 73: | Ligne 80: | ||
| convert_json2csv() | convert_json2csv() | ||
| sys.exit(0) | sys.exit(0) | ||
| - | + | ||
| - | + | ||
| if __name__ == " | if __name__ == " | ||
| main() | main() | ||
| - | |||
| ~~~ | ~~~ | ||
| Ligne 84: | Ligne 90: | ||
| ./ | ./ | ||
| ~~~ | ~~~ | ||
| + | |||
| ## Exemple 2 - liste de dictionnaires | ## Exemple 2 - liste de dictionnaires | ||
| Ligne 94: | Ligne 101: | ||
| `json2csv_list_of_dicts.py` | `json2csv_list_of_dicts.py` | ||
| ~~~python | ~~~python | ||
| - | #! / | + | # |
| - | + | ||
| import sys | import sys | ||
| import json | import json | ||
| import argparse | import argparse | ||
| from datetime import datetime | from datetime import datetime | ||
| - | + | ||
| - | parser = argparse.ArgumentParser( | + | parser = argparse.ArgumentParser(epilog=""" |
| - | | + | |
| parser.add_argument(" | parser.add_argument(" | ||
| - | parser.add_argument(" | + | parser.add_argument(" |
| args = parser.parse_args() | args = parser.parse_args() | ||
| - | + | ||
| def clean_field(field): | def clean_field(field): | ||
| """ | """ | ||
| - | Clean each fields. | + | Clean each fields. |
| CSV use ';' | CSV use ';' | ||
| CSV use line break as line separator and so all ' | CSV use line break as line separator and so all ' | ||
| Ligne 119: | Ligne 126: | ||
| ret = str(field).strip().replace(";", | ret = str(field).strip().replace(";", | ||
| return ret | return ret | ||
| + | |||
| def quote_string(thing): | def quote_string(thing): | ||
| Ligne 125: | Ligne 133: | ||
| else: | else: | ||
| return thing | return thing | ||
| + | |||
| + | |||
| + | def get_date_time(utimestamp): | ||
| + | ts = datetime.fromtimestamp(utimestamp // 1000000) | ||
| + | return ts.date(), ts.time() | ||
| + | |||
| def convert_json2csv(): | def convert_json2csv(): | ||
| Ligne 130: | Ligne 144: | ||
| json_load = json.load(json_file) | json_load = json.load(json_file) | ||
| with open(args.o, | with open(args.o, | ||
| - | headers = json_load[0] | + | headers = json_load[0].copy() |
| - | print(';'.join(headers), | + | headers[" |
| + | del headers[" | ||
| + | print(";".join(headers), | ||
| for elm in json_load: | for elm in json_load: | ||
| - | newtime = elm[' | + | |
| - | newtime | + | |
| - | elm['time_usec'] = newtime | + | for v in elm.values(): |
| - | for v in elm.values(): | + | print(quote_string(clean_field(v)), |
| - | print(quote_string(clean_field(v)), | + | print(file=csv_file, |
| - | print(file=csv_file, | + | |
| def truncate(): | def truncate(): | ||
| with open(args.o, | with open(args.o, | ||
| csv_file.truncate(0) | csv_file.truncate(0) | ||
| + | |||
| def main(): | def main(): | ||
| Ligne 149: | Ligne 166: | ||
| convert_json2csv() | convert_json2csv() | ||
| sys.exit(0) | sys.exit(0) | ||
| - | + | ||
| - | + | ||
| if __name__ == " | if __name__ == " | ||
| main() | main() | ||
| Ligne 158: | Ligne 175: | ||
| ./ | ./ | ||
| ~~~ | ~~~ | ||
| + | |||
| + | |||
| + | ### Exemple 2b | ||
| + | |||
| + | |||
| + | Bon, il y avait plus simple | ||
| + | ~~~bash | ||
| + | # cat Historique.json | jq -r ' | ||
| + | echo '" | ||
| + | |||
| + | jq --raw-output ' | ||
| + | ." | ||
| + | | [.favicon_url, | ||
| + | | @csv | ||
| + | ' Historique.json >> Historique.csv | ||
| + | ~~~ | ||
| + | |||
tech/script_python_conversion_json_vers_csv.1793949290.txt.gz · Dernière modification : de Jean-Baptiste
