1

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
    },
}
3
  • 1
    JSON objects can't have non-string keys. It seems like you want an array (in Python: list) not an object (dictionary). Commented Aug 20, 2022 at 10:35
  • Thanks jonrsharpe for editing my post Commented Aug 20, 2022 at 10:36
  • Ah, i dont have much experiences working with json, and not really sure which datatype i want to use here since i just started to build this small flashcard GUI. I can see its weird using index number as key for a dictionary. I also dont know many options to solve this so the data will be easy to get and update later on. As u suggested, should i make a list from this data, put it in a file then import it? Commented Aug 20, 2022 at 10:44

1 Answer 1

1

The short answer is that you can't. The JSON standard defines the use of strings for object attribute names. So when you export, python is doing its best to produce useable JSON (by flipping the integer into a string).

The natural solution to this is to pull the CSV file into an array of objects, which will then export cleanly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.