'Creating a JSON Array in Python using contents from a file
Well, I have a list of numbers inside of a file called "codes.txt", and I'm trying to sort them into an array like this:
{
codes: [
"430490",
"348327",
"923489"
]
}
Or just simply ["430490", "348327", "923489"]. How would I do this? I'm not trying to output this into a file, but create a variable that contains that JSON array. I have this so far:
codefile = open('codes.txt', 'r')
codelist = logfile.readlines()
codefile.close()
for line in codelist:
# ?
Solution 1:[1]
This should be enough:
with open('codes.txt', 'r') as codefile:
data = {
'codes': list(codefile)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | md2perpe |
