Reputation: 63
Please help me!! I am still learning data structure and I am trying to convert this CSV file
key,a,b,c,d,e
key_01,apple,100,200,doll,elephant
key_02,apple,100,200,doll,elephant
key_03,apple,100,200,doll,elephant
to Json format. I have tried this script
import csv
import json
csvFilePath = 'my.csv'
jsonFilePath = 'my.json'
def make_json(csvFilePath, jsonFilePath):
data = {}
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
for rows in csvReader:
key = rows["key"]
data[key] = rows
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonf.write(json.dumps(data, indent=4))
make_json(csvFilePath, jsonFilePath)
But the output has key row in it
{
"key_01": {
"key": "01",
"a": "apple",
"y": "100.0",
"c": "cow",
"d": "doll",
"e": "elephant"
},
"key_02": {
"key": "02",
"a": "apple",
"y": "100.0",
"c": "cow",
"d": "doll",
"e": "elephant"
},
}
I want my final output to be look like this. Is there any way to make this right? Please help me.
{
"key_01": {
"a": "apple",
"y": "100.0",
"c": "cow",
"d": "doll",
"e": "elephant"
},
"key_02": {
"a": "apple",
"y": "100.0",
"c": "cow",
"d": "doll",
"e": "elephant"
},
"key_03": {
"a": "apple",
"y": "100.0",
"c": "cow",
"d": "doll",
"e": "elephant"
},
Upvotes: 1
Views: 377
Reputation: 1213
You can use the pandas
library if you have access to external libraries. It makes this task very straightforward:
import pandas as pd
# We don't read the key column.
df = pd.read_csv("my.csv", usecols=["a", "b", "c", "d", "e"])
# We create another column here with your desired format.
df["indexer"] = [f"record_{i}" for i in df.index]
# We make it the index, so if you export it to JSON later it will have your desired format.
df.set_index("indexer", inplace=True)
jsn = df.to_json('my.json', orient="index")
# OR, directly to Python dict for further use
dct = df.to_dict(orient="index")
Upvotes: 1
Reputation: 11083
in this part of your code:
for rows in csvReader:
key = rows["key"]
data[key] = rows
del data[key]['key'] # Add this line
Upvotes: 1
Reputation: 29742
Use dict.pop
to remove the desired value:
import csv
import json
from io import StringIO
new_str = """key,a,b,c,d,e
01,apple,100,200,doll,elephant
02,apple,100,200,doll,elephant
03,apple,100,200,doll,elephant
"""
data = {}
csvReader = csv.DictReader(StringIO(new_str))
for rows in csvReader:
key = rows.pop("key")
data[key] = rows
print(json.dumps(data, indent=4))
Output:
{
"01": {
"a": "apple",
"b": "100",
"c": "200",
"d": "doll",
"e": "elephant"
},
"02": {
"a": "apple",
"b": "100",
"c": "200",
"d": "doll",
"e": "elephant"
},
"03": {
"a": "apple",
"b": "100",
"c": "200",
"d": "doll",
"e": "elephant"
}
}
Upvotes: 1