I have a CSV file that has two columns, one for French words and one for English words:
French,English
partie,part
histoire,history
chercher,search
seulement,only
police,police
I tried to change the CSV data to JSON but faced an error. My index, which I wanted to be an integer, appeared to be a string. Tried to use int() also but looks like it won't fix this.
How can I change index values to integer inside the JSON file?
import json
import pandas
data = pandas.read_csv("data/french_words.csv")
words = {int(index): {
"French": row.French,
"English": row.English,
"known": None
} for index, row in data.iterrows()
}
data.update(words)
with open("words.json", mode="w") as words_file:
json.dump(words, words_file, indent=4)
print(words)
Output:
{
0: {'French': 'partie', 'English': 'part', 'known': None},
1: {'French': 'histoire', 'English': 'history', 'known': None},
2: {'French': 'chercher', 'English': 'search', 'known': None},
3: {'French': 'seulement', 'English': 'only', 'known': None},
4: {'French': 'police', 'English': 'police', 'known': None},
5: {'French': 'pensais', 'English': 'thought', 'known': None},
}
The result contains 100 key, value pairs. I just put first 5 here.
JSON file output
{
"0": {
"French": "partie",
"English": "part",
"known": null
},
"1": {
"French": "histoire",
"English": "history",
"known": null
},
"2": {
"French": "chercher",
"English": "search",
"known": null
},
"3": {
"French": "seulement",
"English": "only",
"known": null
},
"4": {
"French": "police",
"English": "police",
"known": null
},
"5": {
"French": "pensais",
"English": "thought",
"known": null
},
}