diff --git a/README.md b/README.md index cfc8b39..9558999 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ -# Convert_json_to_csv +# Преобразование файла JSON в CSV +Этот скрипт принимает файл JSON в качестве входных данных и генерирует файл CSV на выходе. + +``` +python Converter.py +``` \ No newline at end of file diff --git a/converter.py b/converter.py new file mode 100644 index 0000000..0e52649 --- /dev/null +++ b/converter.py @@ -0,0 +1,15 @@ +import json + +if __name__ == '__main__': + try: + with open('input.json', 'r') as f: + data = json.loads(f.read()) + + output = ','.join([*data[0]]) + for obj in data: + output += f'\n{obj["Name"]},{obj["age"]},{obj["birthyear"]}' + + with open('output.csv', 'w') as f: + f.write(output) + except Exception as ex: + print(f'Error: {str(ex)}') \ No newline at end of file diff --git a/input.json b/input.json new file mode 100644 index 0000000..b48d8f8 --- /dev/null +++ b/input.json @@ -0,0 +1,12 @@ +[ + { + "Name": "Akash", + "age": 26, + "birthyear": "1994" + }, + { + "Name": "Abhay", + "age": 34, + "birthyear": "1986" + } +] \ No newline at end of file diff --git a/output.csv b/output.csv new file mode 100644 index 0000000..6449cf9 --- /dev/null +++ b/output.csv @@ -0,0 +1,3 @@ +Name,age,birthyear +Akash,26,1994 +Abhay,34,1986 \ No newline at end of file