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.

94 questions
44
votes
6 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
  • 112,946
  • 110
  • 377
  • 526
39
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
  • 393
  • 1
  • 3
  • 5
16
votes
1 answer

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…
sudoremo
  • 2,274
  • 2
  • 22
  • 39
13
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
  • 3,171
  • 3
  • 28
  • 41
13
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
  • 133
  • 1
  • 1
  • 6
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,554
  • 3
  • 20
  • 29
11
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
  • 213
  • 2
  • 6
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,383
  • 2
  • 15
  • 25
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
  • 10,663
  • 24
  • 75
  • 119
9
votes
2 answers

Split multi-line string with shlex and keep quote characters

How do I split a string with Python's shlex while preserving the quote characters that shlex splits on? Sample Input: Two Words "A Multi-line comment." Desired Output: ['Two', 'Words', '"A Multi-line\ncomment."'] Note the double quotes wrapping…
Petrus Theron
  • 27,855
  • 36
  • 153
  • 287
8
votes
0 answers

pipenv custom scripts on wsl 2: shlex.py instream object has not attribute read

I moved my python project from a native Linux installation to Windows and I tried to run pipenv custom scripts through WSL 2 on Windows 10 using pipenv run my_script. Although the same command works in a native Ubuntu installation, on WSL 2 (Ubuntu…
marinoandrea
  • 81
  • 1
  • 7
8
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
  • 38,111
  • 12
  • 81
  • 101
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
  • 6,032
  • 2
  • 26
  • 26
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,594
  • 2
  • 11
  • 31
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
  • 74,557
  • 19
  • 143
  • 201
1
2 3 4 5 6 7