tech:script_python_conversion_json_vers_csv
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| tech:script_python_conversion_json_vers_csv [2026/11/11 15:13] – 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 143: | Ligne 149: | ||
| print(";" | print(";" | ||
| for elm in json_load: | for elm in json_load: | ||
| - | # newtime = elm[' | ||
| - | # newtime = datetime.fromtimestamp(newtime) | ||
| elm[" | elm[" | ||
| del elm[" | del elm[" | ||
| Ligne 178: | Ligne 182: | ||
| Bon, il y avait plus simple | Bon, il y avait plus simple | ||
| ~~~bash | ~~~bash | ||
| + | # cat Historique.json | jq -r ' | ||
| echo '" | echo '" | ||
tech/script_python_conversion_json_vers_csv.1794406388.txt.gz · Dernière modification : de Jean-Baptiste
