473,386 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Entering strings as user input but interpreting as Python input (sortof)

Hi:

I'm writing a Python program, a hex line editor, which takes in a line
of input from the user such as:
>>cmd = raw_input('-').split()
-e 01 02 "abc def" 03 04
>>cmd
['e', '01', '02', '"abc', 'def"', '03', '04']

Trouble is, I don't want to split the quoted part where the space occurs.

So I would prefer the resulting list to contain:

['e', '01', '02', '"abc def"', '03', '04']

Furthermore, if the user entered:

-e 01 02 "abc \"def\"\r\n" 03 04

I would want the quoted part to be interpreted as if I entered it into
Python itself (recognize escape sequences, and not split at spaces) as:
>>s = '"abc \"def\"\r\n"'
print s
"abc "def"
"
>>>
In other words, if a quoted string occurs in the user input, I want only
that part to be treated as a Python string. Even more horrifying is
that I want the outer quotes to remain as is (which Python doesn't do,
of course).

I have begun to solve this problem by winding up writing what amounts to
a custom split() method (I call it hsplit(), a function) which is a DFA
that implements some of Python's string lexical analysis. Code shown below.

The point of this in the context of the hex editor is that the user
should be able to enter hex bytes without qualifications like "0xXX" but
rather as simply: "0A 1B 2C" etc. but also be able to input a string
without having to type in hex ASCII codes. Hence the following input
would be valid (the 'e' is the edit command to the editor):

-e 01 02 "a string with newline\n" 3d 4e 5f
-p
Is there a simpler way?

----------------------------------------------------------------
HSTRIP_NONE = 0
HSTRIP_IN_WORD = 1
HSTRIP_IN_QUOTE = 2
HSTRIP_IN_ESC = 3

def hsplit(string):
lst = []
word = []
state = HSTRIP_NONE # not in word
for c in string:

if state == HSTRIP_NONE:
if c == '"':
word.append(c)
state = HSTRIP_IN_QUOTE
elif c != ' ':
word.append(c)
state = HSTRIP_IN_WORD
# else c == ' ', so pass
elif state == HSTRIP_IN_QUOTE:
if c == '"':
word.append(c)
lst.append(''.join(word))
word = []
state = HSTRIP_NONE
elif c == '\\':
state = HSTRIP_IN_ESC
else:
word.append(c)
elif state == HSTRIP_IN_ESC:
if c == '\\':
word.append(c)
state = HSTRIP_IN_QUOTE
elif c == '"':
word.append(c)
state = HSTRIP_IN_QUOTE
elif c == 'n':
word.append('\n')
state = HSTRIP_IN_QUOTE
else: # c == non escape or quote
# for unrecognized escape, just put in verbatim
word.append('\\')
word.append(c)
state = HSTRIP_IN_QUOTE
else: # if state == HSTRIP_IN_WORD
if c == ' ' or c == '"':
lst.append(''.join(word))
if c == '"':
word = [c]
state = HSTRIP_IN_QUOTE
else:
word = []
state = HSTRIP_NONE
else:
word.append(c)
# this only happens if you run out of chars in string before a
state change:
if word: lst.append(''.join(word))
return lst

----------------------------------------------------------------
--
Good day!

________________________________________
Christopher R. Carlen
Principal Laser&Electronics Technologist
Sandia National Laboratories CA USA
cr***************@BOGUSsandia.gov
NOTE, delete texts: "RemoveThis" and
"BOGUS" from email address to reply.
Sep 18 '07 #1
1 2235
En Mon, 17 Sep 2007 21:01:38 -0300, Chris Carlen
<cr***************@BOGUSsandia.govescribi�:
I'm writing a Python program, a hex line editor, which takes in a line
of input from the user such as:
>>cmd = raw_input('-').split()
-e 01 02 "abc def" 03 04
>>cmd
['e', '01', '02', '"abc', 'def"', '03', '04']

Trouble is, I don't want to split the quoted part where the space occurs.
For this, try using the shlex module
<http://docs.python.org/lib/module-shlex.html>
The cmd module may be useful too, so you can concentrate on your own
functions, not the framework: <http://docs.python.org/lib/module-cmd.html>

--
Gabriel Genellina

Sep 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Paul Watson | last post by:
How can I get the escapes from a command line parameter interpreted? The user provides a string on the command line. The string might contain traditional escapes such as \t, \n, etc. It might...
50
by: dataangel | last post by:
I wrote a function to compare whether two strings are "similar" because I'm using python to make a small text adventure engine and I want to it to be responsive to slight mispellings, like...
9
by: Xah Lee | last post by:
© # -*- coding: utf-8 -*- © # Python © © import sys © © nn = len(sys.argv) © © if not nn==5: © print "error: %s search_text replace_text in_file out_file" % sys.argv
1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
10
by: robin | last post by:
hi, i'm doing some udp stuff and receive strings of the form '0.870000 0.250000 0.790000;\n' what i'd need though is a list of the form i got to the part to obtain a string '0.870000 0.250000...
5
by: BBands | last post by:
I'd like to see if a string exists, even approximately, in another. For example if "black" exists in "blakbird" or if "beatles" exists in "beatlemania". The application is to look though a long...
9
by: Eric_Dexter | last post by:
http://www.ddj.com/184405774;jsessionid=BDDEMUGJOPXUMQSNDLQCKHSCJUNN2JVN I saw a warning from homeland security about this. I only comment on the because I am trying to use os.system('command1...
7
by: Carroll, Barry | last post by:
Greetings: Personally, I don't think top-posting is the most annoying newsgroup habit. I think it's making a big fuss about minor inconveniences. One of the nicest things about being human...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.