Questions tagged [shlex]

Use this tag for questions about the python shlex module.

From python.docs:

The shlex class makes it easy to write lexical analyzers for simple syntaxes resembling that of the Unix shell. This will often be useful for writing minilanguages, (for example, in run control files for Python applications) or for parsing quoted strings.

85 questions
43
votes
5 answers

What's the reverse of shlex.split?

How can I reverse the results of a shlex.split? That is, how can I obtain a quoted string that would "resemble that of a Unix shell", given a list of strings I wish quoted? Update0 I've located a Python bug, and made corresponding feature requests…
Matt Joiner
  • 100,604
  • 94
  • 332
  • 495
28
votes
1 answer

What is the difference between shlex.split() and re.split()?

So I used shlex.split() recently to split a command as argument to subprocess.Popen() function. I recalled that long back I also used re.split() function to split a string with a specific delimiter specified. Can someone point out what is the…
swordfish
  • 283
  • 1
  • 3
  • 5
16
votes
2 answers

Python: Split a string, respect and preserve quotes

Using python, I want to split the following string: a=foo, b=bar, c="foo, bar", d=false, e="false" This should result in the following list: ['a=foo', 'b=bar', 'c="foo, bar"', 'd=false', 'e="false'"'] When using shlex in posix-mode and splitting…
Remo
  • 2,160
  • 2
  • 20
  • 33
12
votes
3 answers

shlex.split still not supporting unicode?

According to the documentation, in Python 2.7.3, shlex should support UNICODE. However, when running the code below, I get: UnicodeEncodeError: 'ascii' codec can't encode characters in position 184-189: ordinal not in range(128) Am I doing something…
petr
  • 2,430
  • 3
  • 18
  • 27
11
votes
3 answers

Setting group permissions with python

That is my setup: I have a VirtualMachine (Ubuntu 14.04. LTS), where there is running a PostgreSQL/PostGIS database. With Windows 7 in QGIS I connect to this database and load feature layer into my GIS project. With some python code I create a file…
Stefan
  • 1,011
  • 1
  • 12
  • 23
11
votes
3 answers

How can I split a string into tokens?

If I have a string 'x+13.5*10x-4e1' how can I split it into the following list of tokens? ['x', '+', '13', '.', '5', '*', '10', 'x', '-', '4', 'e', '1'] Currently I'm using the shlex module: str = 'x+13.5*10x-4e1' lexer =…
Martin Thetford
  • 113
  • 1
  • 1
  • 6
10
votes
2 answers

Python shlex.split(), ignore single quotes

How, in Python, can I use shlex.split() or similar to split strings, preserving only double quotes? For example, if the input is "hello, world" is what 'i say' then the output would be ["hello, world", "is", "what", "'i", "say'"].
tekknolagi
  • 9,371
  • 20
  • 66
  • 112
10
votes
6 answers

Removing white space from txt with python

I have a .txt file (scraped as pre-formatted text from a website) where the data looks like this: B, NICKOLAS CT144531X D1026 JUDGE ANNIE WHITE JOHNSON ANDREWS VS BALL JA-15-0050 D0015 JUDGE…
aysha
  • 203
  • 2
  • 6
10
votes
1 answer

python, windows : parsing command lines with shlex

When you have to split a command-line, for example to call Popen, the best practice seems to be subprocess.Popen(shlex.split(cmd), ... but RTFM The shlex class makes it easy to write lexical analyzers for simple syntaxes resembling that of the Unix…
Massimo
  • 2,114
  • 2
  • 24
  • 35
7
votes
2 answers

subprocess.Popen and shlex.split formatting in windows and linux

I am writing a wrapper to automate some android ADB shell commands via Python (2.7.2). Since, in some cases, I need to run the command asynchronously, I am using the subprocess.Popen method to issue shell commands. I have run into a problem with…
Nisan.H
  • 5,334
  • 2
  • 19
  • 25
7
votes
2 answers

Making shlex.split respect UNC paths

I'm using shlex.split to tokenize arguments for a subprocess.Popen call. However, when one of those args is a UNC path, things get hairy: import shlex raw_args = '-path "\\\\server\\folder\\file.txt" -arg SomeValue' args =…
Adam Lear
  • 35,439
  • 12
  • 80
  • 98
7
votes
1 answer

Re-creating a python invocation

Is it possible to patch together a copy-and-pastable invocation for a python program from the invoked program itself? It doesn't have to be exactly the same invocation string, but the arguments should parse to the same thing. Note that '…
VF1
  • 1,441
  • 1
  • 11
  • 30
7
votes
1 answer

Putting shlex in debug mode

I wanted to see if shlex was a good choice for something I'm trying to build, so I thought I'd put it in debug mode to play around with it. Only, shlex's constructor has this weird thing it does: it sets self.debug to 0 and then immediately checks…
kojiro
  • 67,745
  • 16
  • 115
  • 177
7
votes
1 answer

most efficient way to parse this scripting language

I'm implementing an interpreter for a long-outdated text editor's scripting language, and I'm having some trouble getting a lexer to work properly. Here's an example of the problematic part of the language: T L /LOCATE ME/ C /LOCATE ME/CHANGED ME/ *…
Robbie Rosati
  • 1,147
  • 1
  • 8
  • 22
6
votes
1 answer

Python 3 backward compatability (shlex.quote vs pipes.quote)

One of my projects uses shlex.quote which is available since python 3.3. But that shlex.quote is the same as pipes.quote which is deprecated after moving to shlex. Now for compatibility I am using this code: def cmd_quote(string): import sys …
Victor Polevoy
  • 12,735
  • 7
  • 68
  • 134
1
2 3 4 5 6