Skip to content

Commit c784356

Browse files
committed
added csv to json converter script
1 parent 1e66d3f commit c784356

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

csv_to_json/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!--Please do not remove this part-->
2+
![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99)
3+
![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)
4+
5+
# CSV TO JSON
6+
7+
## 🛠️ Description
8+
<!--Remove the below lines and add yours -->
9+
This script helps to convert a csv file to a json file.
10+
11+
## ⚙️ Languages or Frameworks Used
12+
<!--Remove the below lines and add yours -->
13+
language used - python3.
14+
Packages required - csv and json.
15+
if not already installed use pip3 install csv and pip3 install json.
16+
(If there are a lot of them, including a `requirements.txt` file will work better.)
17+
18+
## 🌟 How to run
19+
<!--Remove the below lines and add yours -->
20+
python3 csv_to_json.py.
21+
22+
23+
24+
## 🤖 Author
25+
<!--Remove the below lines and add yours -->
26+
Rajit Gupta.

csv_to_json/csv_to_json.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import csv
2+
import json
3+
4+
# Function to convert csv to json
5+
def csv_to_json(file_name):
6+
with open(file_name , 'r') as csv_file:
7+
csv_data=csv.DictReader(csv_file)
8+
data_list=[row for row in csv_data]
9+
json_data = json.dumps(data_list, indent=4)
10+
11+
with open('data.json','w') as json_file:
12+
json_file.write(json_data)
13+
14+
# main function
15+
def main():
16+
file_name=input()
17+
csv_to_json(file_name)
18+
19+
if __name__ == '__main__':
20+
main()

0 commit comments

Comments
 (0)