What would be a good way to handle backslashes with escaped characters?

I have a line in the following format:

s="part1,part2,part3,part4" 

I can split the string into parts by simply calling the s.split(",") command.

Now the question is, what if I have a backslash in a string? Assuming I have the following line,

 s="part1,part2,pa\\,rt3,part4" 

I would like to get the result ["part1","part2","pa,rt3","part4"] .

It was originally intended to replace \, nonexistent string, then split the string with the split command and replace the nonexistent string with a comma.

Can you come up with a better way to deal with this problem?

+4
source share
3 answers

Replacing it with a nonexistent string is a good option.

Otherwise, you can use a regex with a negative lookbehind as follows:

 re.split(r'(?<!\\),', 'part1,part2,pa\\,rt3,part4') 
+11
source

The csv module can handle this:

 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'] 
+4
source

BTW, '\' is not an escape character for a comma. So your line will have a legal word with '\'. If you specifically want \ to be part of this word, then regex-based solutions look good to me.

-1
source

Source: https://habr.com/ru/post/1339432/


All Articles