Page 1 of 1

storing data int .txt or .json files

Posted: 01 Nov 2018, 19:18
by Lea
I can save information on a JSON file, however, I can only read it as a string.

For example below is a list with subjects along with their level and user preference. I would like to be able to retrive the data below as a list rather than a string after it is saved on the JSON file.

Code: Select all

[["TOK", 0, 0], ["spa", 1, 1], ["eng", 1, 0], ["csc", 2, 2], ["mat", 2, 1], ["eco", 2, 2], ["phy", 1, 0]]
I could parse all the information in the list as elements in a list but this would be extremely tedious.

Is there a more efficient way of actually storing variables, in this case lists, so that I can also retrieve them as lists and use them accordingly in python?

Re: storing data int .txt or .json files

Posted: 01 Nov 2018, 20:24
by Mr. MacKenty
Lea,

Awesome question.

I'm not 100% sure what you want your output to be. I understand your input is a JSON type. Do you want to convert the JSON to a list?

Warmly,

Mr. MacKenty
Lea wrote:
01 Nov 2018, 19:18
I can save information on a JSON file, however, I can only read it as a string.

For example below is a list with subjects along with their level and user preference. I would like to be able to retrive the data below as a list rather than a string after it is saved on the JSON file.

Code: Select all

[["TOK", 0, 0], ["spa", 1, 1], ["eng", 1, 0], ["csc", 2, 2], ["mat", 2, 1], ["eco", 2, 2], ["phy", 1, 0]]
I could parse all the information in the list as elements in a list but this would be extremely tedious.

Is there a more efficient way of actually storing variables, in this case lists, so that I can also retrieve them as lists and use them accordingly in python?

Re: storing data int .txt or .json files

Posted: 04 Nov 2018, 21:18
by Lea
Basically what I am asking is:

Is there a way to store variables such as a list in a txt or JSON file that allows me to then access it as a list and not as plain text?

Because that is what I always retrieve

When I

Code: Select all

list = (profile_txt.read())
print(list[0])

# it returns: [
since it assumes what's in the file is plain text. I tried making my list part of a JSON object but it says:

Code: Select all

 '_io.TextIOWrapper' object has no attribute '_json'
It makes retrieving variables from an external file very tedious and complicated since I cannot retrieve it as my variable list.
Mr. MacKenty wrote:
01 Nov 2018, 20:24
Lea,

Awesome question.

I'm not 100% sure what you want your output to be. I understand your input is a JSON type. Do you want to convert the JSON to a list?

Warmly,

Mr. MacKenty
Lea wrote:
01 Nov 2018, 19:18
I can save information on a JSON file, however, I can only read it as a string.

For example below is a list with subjects along with their level and user preference. I would like to be able to retrive the data below as a list rather than a string after it is saved on the JSON file.

Code: Select all

[["TOK", 0, 0], ["spa", 1, 1], ["eng", 1, 0], ["csc", 2, 2], ["mat", 2, 1], ["eco", 2, 2], ["phy", 1, 0]]
I could parse all the information in the list as elements in a list but this would be extremely tedious.

Is there a more efficient way of actually storing variables, in this case lists, so that I can also retrieve them as lists and use them accordingly in python?