Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

escaping - Passing meta-characters to Python as arguments from command line

I'm making a Python program that will parse the fields in some input lines. I'd like to let the user enter the field separator as an option from the command line. I'm using optparse to do this. I'm running into the problem that entering something like will separate literally on , rather than on a tab, which is what I want. I'm pretty sure this is a Python thing and not the shell, since I've tried every combo of quotes, backslashes, and t's that I can think of.

If I could get optparse to let the argument be plain input (is there such a thing?) rather than raw_input, I think that would work. But I have no clue how to do that.

I've also tried various substitutions and regex tricks to turn the string from the two character "" into the one character tab, but without success.

Example, where input.txt is:

field 1[tab]field2

(Note: [tab] is a tab character and field2 is an 8 character string)

parseme.py:

#!/usr/bin/python
from optparse import OptionParser  
parser = OptionParser()  
parser.add_option("-d", "--delimiter", action="store", type="string",  
    dest="delimiter", default='')  
parser.add_option("-f", dest="filename")  
(options, args) = parser.parse_args()  
Infile = open(options.filename, 'r')  
Line = Infile.readline()  

Fields = Line.split(options.delimiter)  
print Fields[0]  
print options.delimiter  

Infile.close()  

This gives me:

$ parseme.py -f input.txt  
field 1  
[tab]

Hey, great, the default setting worked properly. (Yes, I know I could just make the default and forget about it, but I'd like to know how to deal with this type of problem.)

$ parseme.py -f input.txt -d ''  
field 1[tab]field  

This is not what I want.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
>>> r'
v
'.decode('string-escape')
'
x0b
'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...