Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 inverted commas? The only constant would be their position in the file relative to the data lines themselves. The data lines could range from 10 to 100+ characters (they'll be nested network folders). I cannot see how I can use any other way to do those markers to split on, but my lack of python knowledge is making this difficult. I've tried

optfile=line.split("")

and other variations but keep getting valueerror: empty seperator. I can see why it's saying that, I just don't know how to change it. Any help is, as always very appreciated.

Many thanks

like image 563
user2377057 Avatar asked May 17 '13 07:05

user2377057


People also ask

How do you get a string between two quotes in Python?

To extract strings in between the quotations we can use findall() method from re library.

How do I get strings between single quotes in Python?

Use the re. findall() method to extract strings between quotes, e.g. my_list = re. findall(r'"([^"]*)"', my_str) .

How do you split a string with double quotes?

split("(? =\"[^\"]. *\")");


1 Answers

No regex, no split, just use csv.reader

import csv

sample_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 /', '-']
like image 167
mckelvin Avatar answered Oct 28 '22 08:10

mckelvin