'Parsing input copied from a Windows UI on Windows newline characters

I am trying to parse the following input, copied from a UI if that matters, on Windows newline characters (appears to be \r\n) in Python 3.8.

"sample_key" : {
      "price" : {
        "k1" : [ "2Y" ],
        "k2" : [ "3Y" ],
        "Active 5yr" : [ "5Y", "ZF${futuresSuffix}" ],
        "Active 7yr" : [ "7Y", "10Y", "ZN${futuresSuffix}" ],
        "Active 10yr" : [ "7Y", "10Y", "ZN${futuresSuffix}", "ZB${futuresSuffix}" ],
        "Active 20yr" : [ "20Y", "ZB${futuresSuffix}", "UB${futuresSuffix}" ],
        "Active 30yr" : [ "30Y", "UB${futuresSuffix}" ],
        "Off-the-Run 2yr-3yr" : [ "2Y", "3Y" ],
        "Off-the-Run 4yr" : [ "3Y" ],
        "Off-the-Run 5yr" : [ "5Y", "ZF${futuresSuffix}" ],
        "Off-the-Run 6yr" : [ "5Y", "7Y", "ZN${futuresSuffix}" ],
        "Off-the-Run 7yr" : [ "7Y", "ZN${futuresSuffix}" ],
        "Off-the-Run 10yr" : [ "7Y", "10Y", "ZN${futuresSuffix}" ],
        "Off-the-Run 20yr" : [ "20Y", "ZB${futuresSuffix}", "UB${futuresSuffix}" ],
        "Off-the-Run 30yr" : [ "30Y", "ZB${futuresSuffix}", "UB${futuresSuffix

I have tried the following regexes and methods separately, but for some reason the input isn't properly parsed and the length of the resulting tokens list is always 1.

tokens = txt.split('\r\n')
tokens = txt.split('\n')
tokens = txt.split('\\n')
tokens = tmp.splitlines()
tokens = tmp.split('\\r\\n')
tmp = tmp.replace("\r\n", "\n").replace("\r", "\n")
tokens = tmp.splitlines()

Does anyone see the problem?



Solution 1:[1]

Maybe you take your input as dict? Because I have no problem to parse it as a string. Let me know if it is helpfull. enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anytokin