Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c40ea4e

Browse files
Merge pull request avinashkranjan#1684 from prernamittal/csvToJson
Added csv to json script
2 parents eee8fb7 + 0bc2563 commit c40ea4e

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

‎CSVtoJSON script/CSVtoJSON.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import csv
2+
import json
3+
4+
def csv_to_json(csv_file, json_file):
5+
# Open the CSV file
6+
with open(csv_file, 'r') as file:
7+
# Read the CSV data
8+
csv_data = csv.DictReader(file)
9+
10+
# Convert CSV to JSON
11+
json_data = json.dumps(list(csv_data), indent=4)
12+
13+
# Write the JSON data to a file
14+
with open(json_file, 'w') as json_file:
15+
json_file.write(json_data)
16+
17+
# Specify the CSV and JSON file paths
18+
csv_file = 'input.csv'
19+
json_file = 'output.json'
20+
21+
# Convert CSV to JSON
22+
csv_to_json(csv_file, json_file)
23+
24+
print(f"CSV file '{csv_file}' has been converted to JSON file '{json_file}'.")

‎CSVtoJSON script/input.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Name,Age,City
2+
John,25,New York
3+
Jane,30,San Francisco

‎CSVtoJSON script/output.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"Name": "John",
4+
"Age": "25",
5+
"City": "New York"
6+
},
7+
{
8+
"Name": "Jane",
9+
"Age": "30",
10+
"City": "San Francisco"
11+
}
12+
]

‎CSVtoJSON script/readme.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# CSV to JSON converter script
2+
3+
## Input
4+
<li>Create an input.csv file with top row containing column names and rows below that as contents under corresponding columns. Each column should be comma-separated.
5+
<li>Create an empty output.json file.
6+
<li>Edit the file path defined by the variables csv_file and json_file and run the script.
7+
8+
## Output
9+
<li>output.json file will contain the data of input.csv file in json format.
10+
11+
## Author(s)
12+
[Prerna Mittal](https://github.com/prernamittal)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /