-5

I have a string like this:

['过\r\n啤酒\r\n小心\r\n照顾\r\n锻炼\r\n过去\r\n忘记\r\n哭\r\n包\r\n个子\r\n瘦\r\n选择\r\n奶奶\r\n突然\r\n节目\r\n']

How do I remove all of the "\r\n", and then turn the string into a list like so:

[过, 啤酒, 小心, 照顾, 过去, etc...]
Bryan Thai
  • 179
  • 1
  • 2
  • 8
  • 1
    For the string itself, you can use `.split()` if none of those characters are whitespace. – dawg Jun 23 '18 at 14:50
  • Welcome to Stackoverflow, Bryan. Please make sure to read ["how do I ask a good question"](/help/how-to-ask), as it explains not just how to write your question, but also when it's not appropriate yet to ask one. If you've not done any searching for the answer first, and you've not tried *anything* yet, then it's not time to post to Stackoverflow yet. You're not the first person to want to do this, you can bet good coffee on the fact that someone already asked this and got an answer: find that answer. – Mike 'Pomax' Kamermans Jun 23 '18 at 15:20
  • @coldspeed: I disagree the [marked answer](https://stackoverflow.com/questions/16566268/remove-all-line-breaks-from-a-long-string-of-text) is a duplicate. In that case, it is how to REMOVE all line breaks yet keep the result a string. This question (based on the example) is how to split the string on line breaks with the results being elements in a list. Similar, but different enough. I did spend a moment or two looking for a better dup, and I am sure there probably is one, but simply removing the line breaks with `.replace()` is not it I think. – dawg Jun 24 '18 at 01:13

3 Answers3

0

str.split removes all whitespace; this includes \r and \n:

A = ['过\r\n啤酒\r\n小心\r\n照顾\r\n锻炼\r\n过去\r\n忘记\r\n哭\r\n包\r\n个子\r\n瘦\r\n选择\r\n奶奶\r\n突然\r\n节目\r\n']

res = A[0].split()

print(res)

['过', '啤酒', '小心', '照顾', '锻炼', '过去', '忘记', '哭', '包', '个子', '瘦', '选择', '奶奶', '突然', '节目']

As described in the str.split docs:

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.

jpp
  • 159,742
  • 34
  • 281
  • 339
0

To limit the split to \r\n you can use .splitlines():

>>> li=['过\r\n啤酒\r\n小心\r\n照顾\r\n锻炼\r\n过去\r\n忘记\r\n哭\r\n包\r\n个子\r\n瘦\r\n选择\r\n奶奶\r\n突然\r\n节目\r\n']
>>> li[0].splitlines()
['过', '啤酒', '小心', '照顾', '锻炼', '过去', '忘记', '哭', '包', '个子', '瘦', '选择', '奶奶', '突然', '节目']
dawg
  • 98,345
  • 23
  • 131
  • 206
0

Try this:

s = "['过\r\n啤酒\r\n小心\r\n照顾\r\n锻炼\r\n过去\r\n忘记\r\n哭\r\n包\r\n个子\r\n瘦\r\n选择\r\n奶奶\r\n突然\r\n节目\r\n']"
s = s.replace('\r\n', ',').replace("'", '')
print(s)

Output:

[过,啤酒,小心,照顾,锻炼,过去,忘记,哭,包,个子,瘦,选择,奶奶,突然,节目,]

This first replace replaces the \r\n and the second one replaces the single quote from the string as you expected as the output.

Sudarshan
  • 938
  • 14
  • 27