I have a string in the following format;
s="part1,part2,part3,part4"
I can split the string into pieces by just invoking the s.split(",")
command.
Now, the question is what if I have a backslash escaped comma in the string? Assuming I have the following string,
s="part1,part2,pa\\,rt3,part4"
I'd like to be able to get ["part1","part2","pa,rt3","part4"]
as the result.
What I initially thought was to replace the \,
with a non-existent string, then split the string by using the split command and replace the non-existent string with a comm开发者_开发问答a.
Can you think of a better way to deal with this problem?
Replacing it with a non-existing string is a nice option.
And otherwise, you could use a regular expression with a negative lookbehind like this:
re.split(r'(?<!\\),', 'part1,part2,pa\\,rt3,part4')
The csv module can handle this as well:
import csv
from io import StringIO
s = 'part1,part2,pa\\,rt3,part4'
f = StringIO(s)
r = csv.reader(f,quoting=csv.QUOTE_NONE,escapechar='\\')
for row in r:
print row
Output
['part1', 'part2', 'pa,rt3', 'part4']
BTW, '\' is not an escape character for ',' comma. So your string would have have a legal word with '\'. If you specially want the \, to be part of the word, then a regex based solutions looks good to me.
精彩评论