Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

python - Splitting string when the parameter is backslash

I have been trying to do a very simple task but seems pretty hard. I am trying to split a string at the backslash ('\'). I have found few similar posts to this topic but no one was actually addressing a comprehensive issue.

This is the string I want to chop up:

'13
Bono
Goalkeeper
Apr 5, 1991 (29)
€15.00m  '

The problem is if I use .split('\') with the string above I get a syntax error:

SyntaxError: EOL while scanning string literal

I have also tried:

  • Double backslash \
  • Triple quotes """\"""

But it doesn't work either.

I know I can add r in front of a string but would not be suitable as the string is part of a huge list and would have to add to each string the r in front of it.

In conclusion, is there a simple way to simply chop up words using the backslash as the delimiter?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you have to do:

'your string'.split('
')

because you don't have a backslesh '\' but a newline escape charater ' '


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...