Convert JSON to CSV

This commit is contained in:
2024-05-10 22:19:42 +03:00
parent d0d99f69c5
commit c69d01b5bd
4 changed files with 36 additions and 1 deletions

View File

@@ -1,2 +1,7 @@
# Convert_json_to_csv
# Преобразование файла JSON в CSV
Этот скрипт принимает файл JSON в качестве входных данных и генерирует файл CSV на выходе.
```
python Converter.py
```

15
converter.py Normal file
View File

@@ -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)}')

12
input.json Normal file
View File

@@ -0,0 +1,12 @@
[
{
"Name": "Akash",
"age": 26,
"birthyear": "1994"
},
{
"Name": "Abhay",
"age": 34,
"birthyear": "1986"
}
]

3
output.csv Normal file
View File

@@ -0,0 +1,3 @@
Name,age,birthyear
Akash,26,1994
Abhay,34,1986
1 Name age birthyear
2 Akash 26 1994
3 Abhay 34 1986