Skip to content Skip to sidebar Skip to footer

Python Split String On Quotes

I'm a python learner. If I have a lines of text in a file that looks like this 'Y:\DATA\00001\SERVER\DATA.TXT' 'V:\DATA2\00002\SERVER2\DATA2.TXT' Can I split the lines around the

Solution 1:

I'll just add that if you were dealing with lines that look like they could be command line parameters, then you could possibly take advantage of the shlex module:

import shlex

withopen('somefile') as fin:
    for line in fin:
        print shlex.split(line)

Would give:

['Y:\\DATA\\00001\\SERVER\\DATA.TXT', 'V:\\DATA2\\00002\\SERVER2\\DATA2.TXT']

Solution 2:

No regex, no split, just use csv.reader

importcsvsample_line='10.0.0.1 foo "24/Sep/2015:01:08:16 +0800" www.google.com "GET /" -'

def main():
    for l in csv.reader([sample_line], delimiter=' ', quotechar='"'):
        print l

The output is

['10.0.0.1', 'foo', '24/Sep/2015:01:08:16 +0800', 'www.google.com', 'GET /', '-']

Solution 3:

shlex module can help you.

import shlex

my_string = '"Y:\DATA\00001\SERVER\DATA.TXT""V:\DATA2\00002\SERVER2\DATA2.TXT"'
shlex.split(my_string)

This will spit

['Y:\\DATA\x0001\\SERVER\\DATA.TXT', 'V:\\DATA2\x0002\\SERVER2\\DATA2.TXT']

Reference: https://docs.python.org/2/library/shlex.html

Solution 4:

Finding all regular expression matches will do it:

input=r'"Y:\DATA\00001\SERVER\DATA.TXT" "V:\DATA2\00002\SERVER2\DATA2.TXT"'

re.findall('".+?"', # or '"[^"]+"', input)

This will return the list of file names:

["Y:\DATA\00001\SERVER\DATA.TXT", "V:\DATA2\00002\SERVER2\DATA2.TXT"]

To get the file name without quotes use:

[f[1:-1] for f in re.findall('".+?"', input)]

or use re.finditer:

[f.group(1) for f in re.finditer('"(.+?)"', input)]

Solution 5:

The following code splits the line at each occurrence of the inverted comma character (") and removes empty strings and those consisting only of whitespace.

[s for s in line.split('"') if s.strip() != '']

There is no need to use regular expressions, an escape character, some module or assume a certain number of whitespace characters between the paths.

Test:

line = r'"Y:\DATA\00001\SERVER\DATA.TXT" "V:\DATA2\00002\SERVER2\DATA2.TXT"'
output = [s for s in line.split('"') if s.strip() != '']
print(output)
>>> ['Y:\\DATA\\00001\\SERVER\\DATA.TXT', 'V:\\DATA2\\00002\\SERVER2\\DATA2.TXT']

Post a Comment for "Python Split String On Quotes"