PLearner PLearner - 1 year ago 50
Python Question

Python Regular Expression with escape, quote, and delimiter characters

I am new to python and trying to solve the below with regular expression or with CSV reader.

My Input string is in the below format:

"some text"|"sample\" name|place\\""|"some other text\\""

Expected output is:

'some text','sample" name|place\"','some other text\"'

My string has a delimiter, escape character and quote characters. When I save the input text in a file and read it using csv reader, it is working as expected.

with open('inputfile.csv') as csvfile:
inputValue = csv.reader(csvfile, delimiter='|', quotechar='"',escapechar = '\\')
for eachVal in inputValue:

But when I put the input value in a list and use CSV reader it is not giving the correct output.

inputText = '"some text"|"sample\" name|place\\""|"some other text\\""'
inputValue = csv.reader(inputText, delimiter='|',quotechar='"', escapechar = '\\')
for eachVal in inputValue:

Any help with this CSV reader or any solution with regex will be great. Thank You.

Answer Source

When you read a string from a file, you're reading the "raw" text, meaning that Python doesn't provide special treatment for backslash characters etc. To get the same treatment for string literals in your code, you should prefix the string with an 'r' (for raw). For example:

inputText = r'"some text"|"sample\" name|place\\""|"some other text\\""'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download