Question

I have a long string that contains many \n escape sequences that are supposed to be new line characters. To properly write this string as it were meant to be read, I thought it would be best to split the string based on the \n character and then write each string in the resulting list individually to achieve the desired effect. However, this doesn't work, it is just not splitting them correctly. Below is my code and just to be clear I have tried both \n and \n as splits because I am trying to split at a literal \n in the string. Thanks for any help.

shellreturn = subprocess.check_output(["C:\Python34\python",root.wgetdir + "\html2text.py", keyworddir + "\\" + item])
print(shellreturn)
shelllist = (str(shellreturn).split("\\n"))
Was it helpful?

Solution

You have bytes and not str here. Decode it to string like

shellreturn = shellreturn.decode()

or

shellreturn = str(shellreturn, 'utf-8')

After it's decoded you can use .split('\n') or .splitlines().

OTHER TIPS

shellreturn = subprocess.check_output(["C:\Python34\python",root.wgetdir + "\html2text.py", keyworddir + "\\" + item])
print(shellreturn)
shelllist = (str(shellreturn).split("\\n"))

The argument to subprocess.check_output is begging for trouble by not properly escaping \ and not using os.path.join, but that's not what the question is about. You did escape the \ in "\\" as well as "\\n". Let's look at the sample data and what would happen to it:

b"PMSI Direct \n262 Old New Brunswick Rd., Unit M \nPisca..."

The b" mark shows that this is bytes in a Python literal syntax. That means the \ escape sequences are escape sequences, unlike in raw strings (r prefix). So the line delimiter here is "\n", not "\\n". If you split that for "\\n", it will not find any, so you get the original string as the only item in a list. This is a correct split when the delimiter is not found.

An additional complication is that you appear to be running on Windows, where '\n' is not the OS format for newline. They use '\r\n', which Python usually handles behind the scenes when you open text files, so the way you open text_file also matters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top