storing data int .txt or .json files

This is place to ask questions, get clarifications, and ask for help about your internal assessment.
Forum rules
Please be careful about sharing personal information. Never use any identifiable information.
Post Reply
User avatar
Lea
New User
Posts: 3
Joined: 22 May 2018, 08:36

storing data int .txt or .json files

Post by Lea » 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?
Last edited by Lea on 26 Nov 2018, 20:52, edited 1 time in total.
1 x

User avatar
Mr. MacKenty
Site Admin
Posts: 88
Joined: 28 Apr 2018, 17:06
Answers: 3

Re: storing data int .txt or .json files

Post by Mr. MacKenty » 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?
0 x

User avatar
Lea
New User
Posts: 3
Joined: 22 May 2018, 08:36

Re: storing data int .txt or .json files

Post by Lea » 04 Nov 2018, 21:18

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?
0 x

Post Reply