'Trouble replacing instances of "\\n" with "\n" in python, utf8
with open('Datasets/[657667769831260191].txt', encoding="utf8") as f:
textdata = f.read()
##TODO
textdata.replace(r'\\n', '\n')
usermessages = textdata.split(" ")
I'm having trouble replacing instances of "\\n" in a text file I've loaded into a JSON database. As part of cleaning the raw text there seem to be plenty of instances of "\n" and "\\n" the former I can deal with, however I cant seem to find a way for it to recognize literal instances of \\n.
Here is an example
"texthistory": {
"2022-02-02 16:59:10.313359": " Is it ok to pour milk before cereal",
"2022-02-02 16:59:15.313359": " yes",
"2022-02-02 16:59:37.313359": " so I can't eat cereal straight from the box\\nlame\\nI hate social norms",
as you can see there should be two more chat instances, instead they are left alone.
Solution 1:[1]
The answer ended up being a combination of multiple comments.
with open('Datasets/[657667769831260191].txt', encoding="utf8") as f:
textdata = f.read()
textdata = textdata.splitlines()
nl = '\n'
textdata = nl.join(textdata)
textdata = textdata.replace('\\n', '\n')
usermessages = textdata.split(" ")
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 | Seymour |