1

This is killing me. So I have a long string, validated in JSONlint, that only works when loaded as a raw string.

raw_string = r'...'
normal_string = '...'
normal_string = r'%s' % normal_string
data = json.loads(raw_string) #<== works
data = json.loads(string) #<== doesn't

I cannot get the string to be interpreted as raw string as it runs. I believe there is something fishy about this string because there is an error no matter how the problem is approached. (encoding, decoding, etc)

You can see the work here: https://repl.it/@dennis_pitt/Interpret-String-as-Raw

Please point me in the right direction. Python3.5

Dennis Pitt
  • 89
  • 1
  • 1
  • 9
  • 2
    `r` prefix is only for literals. you cannot build a raw string from an already interpreted one – Jean-François Fabre Jan 30 '18 at 19:40
  • 1
    Whatever digraph was intended to be protected in `raw_string` has already been processed by the time you try to use `r'%s' % normal_string`. – chepner Jan 30 '18 at 19:41
  • I think there's a duplicate somewhere. But you would have to provide a [mcve], not this huge replit dump. – Jean-François Fabre Jan 30 '18 at 19:42
  • when I simplify the string, it works. It is the string in its entirety that causes the problem. However, the data is needed from the entire string. – Dennis Pitt Jan 30 '18 at 19:46
  • 1
    Just so we're clear here - there's no such thing as a raw string. There's a raw string *literal*, which has different rules for how the Python source is converted to an actual string. `r'\t'` and `'\\t'` are *the same string* for example. – Mark Ransom Jan 30 '18 at 19:53
  • The problem is that your string has a lot of ` \ ` characters in it. In a raw string literal those will be left as-is, in a normal string literal they will swallow up the following character(s) and become a completely different character. This is exactly why raw string literals were invented, why can't you use them? – Mark Ransom Jan 30 '18 at 20:03
  • that string is part cut out of a larger string scraped via scrapy xpath. I have to manipulate the string a bit to remove the extra data, then want to turn it into a usable object. – Dennis Pitt Jan 30 '18 at 22:09
  • If you didn't get the string from a literal but scraped it, then it should already be a usable object. Your problem isn't where you think it is. – Mark Ransom Jan 30 '18 at 22:41

0 Answers0