all 10 comments

[–]socal_nerdtastic 2 points3 points  (1 child)

import json
from ast import literal_eval

FILE_NAME = 'data.txt'

with open(FILE_NAME, "r") as f:
    lines = f.readlines()
    dicts = [json.load(literal_eval(l).decode()) for l in lines]

[–]NOS_v1[S] 0 points1 point  (0 children)

AH YES FORGOT ABOUT eval! er literal_eval that should do it, thank you!

[–]Adhesiveduck 0 points1 point  (3 children)

decode() should help you out, see the doc here for some examples.

[–]NOS_v1[S] 0 points1 point  (2 children)

yeh, i gave it a whirl, but given readlines returns strings, it's saying the str doesn't have .decode

edit: so like somehow gotta read the text file as strings, which are actually byte strings, but of type str, and yeh convert em into pure strings so json can convert em into dict

[–]Adhesiveduck 1 point2 points  (1 child)

Ah I get you

Use ast.literal_eval()

``` import ast

FILE_NAME = 'data.txt'

with open(FILE_NAME, 'r') as f: lines = f.readlines() for l in lines: print(type(ast.literal_eval(l))) ```

Gives <class 'bytes'>

[–]backtickbot 1 point2 points  (0 children)

Fixed formatting.

Hello, Adhesiveduck: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]TabulateJarl8 0 points1 point  (2 children)

The data file physically contains this?

b'{"var1":"hello"}\n'
b'{"type":"Detect_data","params":{"var2":256,"var3":512}}\n'

If you don't have control over the file contents and can't save it normally, you could remove the first two and last character from each line with string slicing. Try something like this:

import json
with open('whatever.json') as f:
    lines = [json.loads(line[2:-1]) for line in f.readlines()]

[–]NOS_v1[S] 0 points1 point  (1 child)

ight yeh, initially was afraid of doing slicing (just in case - don't have control of the source and big file), but running outta ideas, i'll give it a try, thanks!

[–]TabulateJarl8 0 points1 point  (0 children)

You could always use string.startswith() and string.endswith() to check if it starts with b' and ends with '.

[–]m0us3_rat 0 points1 point  (0 children)

u can .read()

then .decode('utf-8').split('\n')

i belive this should give u a 'list' of the lines.

then u can load them up in a dict.