xxxxxxxxxx
import pandas as pd
# enter the json filename to be converted to json
JSON_FILE = 'json_filename.json'
# enter the csv filename you wish to save it as
CSV_FILE = 'csv_filename.csv'
with open(JSON_FILE, encoding = 'utf-8') as f :
df = pd.read_json(f)
df.to_csv(CSV_FILE, encoding = 'utf-8', index = False)
xxxxxxxxxx
import csv
import json
# Function to convert a CSV to JSON
# Takes the file paths as arguments
def make_json(csvFilePath, jsonFilePath):
# create a dictionary
data = {}
# Open a csv reader called DictReader
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
# Convert each row into a dictionary
# and add it to data
for rows in csvReader:
# Assuming a column named 'No' to
# be the primary key
key = rows['No']
data[key] = rows
# Open a json writer, and use the json.dumps()
# function to dump data
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonf.write(json.dumps(data, indent=4))
# Driver Code
# Decide the two file paths according to your
# computer system
csvFilePath = r'Names.csv'
jsonFilePath = r'Names.json'
# Call the make_json function
make_json(csvFilePath, jsonFilePath)
xxxxxxxxxx
import pandas as pd
df = pd.read_json (r'my_file.json')
# content of my_file.json :
#
# {"Product":{"0":"Desktop Computer","1":"Tablet","2":"Printer","3":"Laptop"},"Price":{"0":700,"1":250,"2":100,"3":1200}}
df.to_csv (r'my_file.csv', index = None)
# content of my_file.json :
#
# Product,Price
# Desktop Computer,700
# Tablet,250
# Printer,100
# Laptop,1200
xxxxxxxxxx
import json
import csv
with open('G:\Akhil\jsonoutput.json') as json_file:
jsondata = json.load(json_file)
data_file = open('G:\Akhil\jsonoutput.csv', 'w', newline='')
csv_writer = csv.writer(data_file)
count = 0
for data in jsondata:
if count == 0:
header = data.keys()
csv_writer.writerow(header)
count += 1
csv_writer.writerow(data.values())
data_file.close()
xxxxxxxxxx
'''
@author : alixaprodev.com
'''
import csv, json
input_json_file='json_file.json'
output_csv_file='csv_file.csv'
input = open(input_json_file)
data = json.load(input)
input.close()
output = csv.writer(open(output_csv_file,'w'))
output.writerow(data[0].keys()) # header row
for row in data:
output.writerow(row.values())
xxxxxxxxxx
import pandas as pd
csv_data = pd.read_csv('data.csv')
json_data = csv_data.to_json(orient = 'records')
with open('json_data.json', 'w') as jsonfile:
jsonfile.write(json_data)
xxxxxxxxxx
import csv
import json
# Open the CSV file and read its contents
with open('BPH pharmacy 2 - Sheet1.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
# Create an empty list to store the data
data = []
# Loop through each row in the CSV file
i=0
for row in csv_reader:
# Get the tag and patterns/responses from the row
print(row)
if row[0]=="Question":
print("No data")
else:
tag = i
i=i+1
patterns = row[0]
responses = row[1].split('|')
# Create a dictionary for the row and add it to the data list
data.append({
"tag": str(tag),
"patterns": patterns,
"responses": responses
})
# Open a JSON file to write the data to
with open('output.json', 'w') as json_file:
# Write the data to the JSON file
json.dump(data, json_file)