Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54937

Re: Weird bahaviour from shlex - line no

Date 2013-09-28 13:02 +0200
From Andreas Perstinger <andipersti@gmail.com>
Subject Re: Weird bahaviour from shlex - line no
References <CAEvez=x1ySjRtR_7asYtU3FoRsC8c1g0g_OZQ_NE-Ok443bFrg@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.408.1380366168.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 28.09.2013 08:26, Daniel Stojanov wrote:
> Can somebody explain this. The line number reported by shlex depends
> on the previous token. I want to be able to tell if I have just popped
> the last token on a line.
>
[SNIP]
>
> second = shlex.shlex("word1 word2,\nword3")

Punctuation characters like the comma are not considered as word 
characters by default and thus are seen as different tokens (consisting 
of only a single character):

 >>> lexer = shlex.shlex("foo, bar, ...")
 >>> token = lexer.get_token()
 >>> while token != lexer.eof:
...   print token
...   token = lexer.get_token()
...
foo
,
bar
,
.
.
.

If you want to treat them as "word" characters you need to add them to 
the string "wordchars" (a public attribute of the "shlex" instance):

 >>> lexer = shlex.shlex("foo.bar, baz")
 >>> lexer.wordchar += '.,'
 >>> print lexer.get_token()
foo.bar,
 >>> print lexer.get_token()
baz

There is also a "debug" attribute (with three different levels: 1, 2, 3; 
default value 0 means no debug output):

 >>> lexer = shlex.shlex("foo, bar, ...")
 >>> lexer.debug = 1
 >>> print lexer.get_token()
shlex: token='foo'
foo
 >>> print lexer.get_token()
shlex: popping token ','
,

Bye, Andreas

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Weird bahaviour from shlex - line no Andreas Perstinger <andipersti@gmail.com> - 2013-09-28 13:02 +0200

csiph-web