0

the list of strings I want to iterate through look like this
{"text":Foo bar\01d"},
out of which I want to extract Foo bar. I tried

f = open ("file.json", "r")
for line in f.readlines():
  #for left side
  leftSide = re.compile(r'([^{"text":].*)')
  mo = leftSide.findall(line)

for line in f.readlines():
  #rightside
  rightSide = re.compile(r'.*[\\01d"}$]')
  mo2 = rightSide.findall(line)

I have also unsuccessfully tried this answer. Is it possible to match both in one regex or extract the data in one piece?

2
  • 2
    Do not use regular expressions for json parsing. There's json module, use it. json.load(f) Commented May 11, 2021 at 12:36
  • okay, but i also have the txt version of the files, will use them Commented May 11, 2021 at 12:38

1 Answer 1

1

Why not using:

import re

f = open ("file.json", "r")

for l in f.readlines():

    print (re.split(r'"text":|\x01d',l)[1])

With:

l = '{"text":Foo bar\01d"},'

print (re.split(r'"text":|\x01d',l)[1])

Output:

Foo bar

Here we use a regex split, which uses left and right side delimiters, and take index 1.

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

4 Comments

You should not parse json using regular expressions.
I know. But see OP's comment in his original post.
I don't understand meaning of those comment, txt version of json file is a text file with json formatted data, imho.
Same here. Sometimes peoples need to make their own understanding ;).

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.