简体   繁体   中英

Preserve quotes when using shlex.split

How can one preserve the quotes around "value with spaces" when using shlex.split()?

s = "SOME_VAR=\"value with spaces\" VAR2=value2"
shlex.split(s)
['SOME_VAR=value with spaces', 'VAR2=value2']

Thank you

Your choice of a Python tool may not be optimal.

How about:

$ cat /tmp/tmp.py
import csv
import StringIO

s = "SOME_VAR=\"value with spaces\" VAR2=value2"
reader = csv.reader(StringIO.StringIO(s), csv.excel)

for i in reader:
  print i

$ python /tmp/tmp.py
['SOME_VAR="value with spaces" VAR2=value2']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM