Тёмный

Convert CSV to JSON with Python 

ProProgramming101
Подписаться 1,9 тыс.
Просмотров 37 тыс.
50% 1

Опубликовано:

 

14 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 38   
@krystopher5799
@krystopher5799 6 лет назад
Just what I needed and straight to the point. Thanks man
@proprogramming1018
@proprogramming1018 6 лет назад
Thanks for watching! : )
@j0shda116
@j0shda116 4 года назад
this guy has given me hope of actually passing my comp science class
@faisal.fs1
@faisal.fs1 5 лет назад
Thanks! That works. And specially indent=4 helped me for my large dataset.
@proprogramming1018
@proprogramming1018 4 года назад
Glad it helped! Thanks for watching!
@karthikg5897
@karthikg5897 4 года назад
This is very useful. Thank you. How do I created a nested json? Any suggestions
@mardukvassili
@mardukvassili Год назад
But this import is not based in the standard because every field is an string. In json standard we have boolean, number, string and null
@Kenndy16
@Kenndy16 6 лет назад
Your code has like a sub header. For anyone who wants to get rid of it, use this code: import csv, json csvFilePath = "data.csv" jsonFilePath = "data.js" # Read the CSV and add the data to dictionary ... with open(csvFilePath) as f: reader = csv.DictReader(f) rows = list(reader) # Write data to a JSON file ... with open(jsonFilePath, "w") as f: json.dump(rows, f, indent=4)
@carlosyanez1121
@carlosyanez1121 4 года назад
Thanks for your Time, i was stuck in to it.
@azharkazi7299
@azharkazi7299 4 года назад
Hi... Great coding on conversion of csv to json format.. Well i have a doubt on the output you have got in json format.. Here the numbers cannot be in " ".. also value "True" should be in boolean true, and if there are any cel in your csv file which dosent have any value it should be null. kindly check on this and let me know.. As i used your code to convert my CSV file to Json format. The O/P what i got was not what i was expecting.. My csv file has numbers, boolens, and empty cells... Kindly need help in this regard
@murghay01
@murghay01 6 лет назад
Yo, 1. You're so easy to keep up with with a good voice. hah. 2. They add the shebang operator at top which handles which version of Python you want to use. In your case, it will be v3. can add a 2.7 for older versions, but don't use that.
@proprogramming1018
@proprogramming1018 6 лет назад
Awesome, thanks for the info! And the compliment! : )
@k5xballer
@k5xballer 6 лет назад
Hello, nice Video! Just one Question: Is it somehow possible to acsess only certain columns within the csv file? Thanks for your help!
@proprogramming1018
@proprogramming1018 6 лет назад
Thanks for watching! Yes, you can access any column via the column name, like: csvRow["column-name"]. So if you want to build out your JSON with only certain columns, then just set those specific properties in your "data" object.
@k5xballer
@k5xballer 6 лет назад
But if I for example have three cols: ID, First Name, Last Name and my for loop Looks as follows: for csvRow in csvReader: ID = csvRow["ID"] csvRow["ID"] = int(csvRow["ID"]) csvRow["Last Name"] = csvRow["Last Name"] data.append(csvRow) It will still insert also the "First Name" column into my "data" object. I`m not really sure where exactly I Need to use the specific properties. Sorry, I´m really struggling with this one. Thanks a lot!
@k5xballer
@k5xballer 6 лет назад
Okay, I just recognized that I can use the del operator to get rid of certain columns :) Thanks a lot!
@proprogramming1018
@proprogramming1018 6 лет назад
Nice!
@AgilityGamer
@AgilityGamer 6 лет назад
Keep up the high quality vids!!
@hannahzxc
@hannahzxc 5 лет назад
Can you explain how the code below reads all your headers and parses it accordingly? hmid = csvRow["hmid"] data[hmid] = csvRow
@lloydtozvireva6739
@lloydtozvireva6739 6 лет назад
good piece of work!! Do you have the reverse tutorila ie convert json file to csv?
@proprogramming1018
@proprogramming1018 6 лет назад
Thank you for watching! Well, the reverse is a bit more tricky and the reason is because CSV data is required to be in a table-like format. Like a 2D array. But JSON data can be any kind of object. So some JSON files simply cannot be converted to CSVs. But if you have a JSON file that CAN definitely be converted to CSV, you would do something like this: 1) Read the JSON file: with open('file.json') as jsonFile: jsonData = json.load(jsonFile) 2) Write the CSV using csv.writer You should be able to access the data from jsonData. You might want to convert it to a 2D array. Then you can write it out using the csv writer. More details here: docs.python.org/3/library/csv.html Hope that helps! Sorry I don't have a specific tutorial video, but I'll consider doing one in the future.
@sowiain713
@sowiain713 5 лет назад
with my program, only the first entry in data is written into the json file. however, the csv file has hundreds of entries. i strictly followed the instructions in vieo. what could be the reason?
@proprogramming1018
@proprogramming1018 5 лет назад
Hi, there could be several reasons, but it's difficult to tell without knowing more information. It could be the CSV file is in a format that the script is unable to handle - for example, maybe the line breaks are different than expected, or maybe there are some delimiters getting mixed up. Try stepping through the code as it's reading the file and this may help you identify what's going on.
@ruchirathore6586
@ruchirathore6586 5 лет назад
Hie, i was able to read my csv file, can you explain how to convert them to avro instead of json?
@yacinefetouh1011
@yacinefetouh1011 5 лет назад
Nice tuto ! How about csv to yaml using python ?
@tintlwinsoe5083
@tintlwinsoe5083 4 года назад
use that code and got Error 13 : Permission Denied Error. Can explain ? Anyone ?
@hannahzxc
@hannahzxc 5 лет назад
what if there is no header for columns?
@proprogramming1018
@proprogramming1018 5 лет назад
Hi, good question! In this case, what you should do is first create an array with all of the property names. Then, when you create the csv.DictReader, pass in the property names as the "fieldnames" argument. So, for this video example, you would do this: fieldnames = ["hmid","wid","reflection_period","original_hm","cleaned_hm","modified","num_sentence","ground_truth_category","predicted_category"] with open(csvFilePath) as csvFile: csvReader = csv.DictReader(csvFile, fieldnames=fieldnames) Then everything else should work the same!
@leejageorge8327
@leejageorge8327 4 года назад
Thank so much. This way its working
@PedroHenrique-qy9ph
@PedroHenrique-qy9ph 2 года назад
pq mandou uma foto?
@jasperzanjani
@jasperzanjani 6 лет назад
😂😂 this guy put a random comment at the top of his python script, from memory no less, which has some random path off somebody's tutorial from years ago probably
@gustavom8726
@gustavom8726 3 года назад
that was fast
Далее
CSV To JSON With Python Pandas
8:16
Просмотров 12 тыс.
How to Convert CSV to JSON in Python
5:20
Просмотров 35 тыс.
Это было очень близко...
00:10
Просмотров 919 тыс.
Handling JSON data with Python
18:44
Просмотров 67 тыс.
Save Excel Table to a JSON File with Simple VBA Macro
20:00
Python - Accessing Nested Dictionary Keys
24:48
Просмотров 184 тыс.
Это было очень близко...
00:10
Просмотров 919 тыс.