0

In a file that I would like to convert to json I have this:

""status"":200,""ts"":1543039907796,""userAgent"":"Mozilla 5.0""..... <- (It's variable)

I would like to replace "" for ", I mean:

"status":200,"ts":1543039907796,"userAgent":"Mozilla 5.0"......

I'm reading a log file with a json format like this:

def process_log_file(cur, filepath):
    # open log file
    with open(filepath) as json_file:
        data = json_file.read().replace('\n', ',').replace('\\"', '').replace('\\/"', '').replace('\/"', ' ').replace('\/', ' ')
        df = pd.read_json(data)

In this line: data = json_file.read().replace('\n', ',').replace('\"', '').replace('\/"', '').replace('/"', ' ').replace('/', ' ')

I've tried with:

replace('""', '"')

replace("""""", "")

replace('(")(")','"')

and It does not work. somebody knows why?

6
  • What output you got after replace('""', '"')? Apr 10, 2021 at 9:57
  • Just nothing change
    – Rrue
    Apr 10, 2021 at 10:03
  • I copied your input and executed string.replace('""', '"'), double double quotes got replace. Apr 10, 2021 at 10:05
  • In my case '""status"":200,""ts"":1543039907796,""userAgent"":"Mozilla 5.0""' it was just an example. I mean is a huge file with json format so is variable... the """ appears just in some places, that file comes from a process and actually is a log file
    – Rrue
    Apr 10, 2021 at 10:09
  • Your actual attempt doesn't contain .replace('""', '"') - did you just forget to put it in the code?
    – tripleee
    Apr 10, 2021 at 10:21

2 Answers 2

4

You can be more intelligent than exact string matches in regex, your regex can look like:

re.sub('"+', '"', '""status"":200,""ts"":1543039907796,""userAgent"":"Mozilla 5.0""')

It will replace all double quotes where there are multiple, outputting this:

'"status":200,"ts":1543039907796,"userAgent":"Mozilla 5.0"'
4
  • In my case '""status"":200,""ts"":1543039907796,""userAgent"":"Mozilla 5.0""' it was just an example. I mean is a huge file with json format so is variable... the """ appears just in some places, that file comes from a process and actually is a log file
    – Rrue
    Apr 10, 2021 at 10:06
  • Ok, so you need to update your question with these details if your objective is not met. As your question in worded currently I believe I answered it Apr 10, 2021 at 10:10
  • But either way if you have this string as a variable and it is a huge JSON string it should work just the same. Apr 10, 2021 at 10:11
  • Sorry my bad, I've updated my question. thanks.!
    – Rrue
    Apr 10, 2021 at 10:14
0

Your text to be replaced starts with the double quotation marks. Python accepts them as the string symbols. Try following:

text="""
""status"":200,""ts"":1543039907796,""userAgent"":"Mozilla 5.0""
"""
text.replace('""','"')

This yields:

'\n"status":200,"ts":1543039907796,"userAgent":"Mozilla 5.0"\n'

You could get rid of '\n' characters in any way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.