Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.0k 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 Reply

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 ' '


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...