I’ve made a quick python script that converts to Unicode from ASCII and back. I’m taking input via argparse so it has to be contained within quotation marks, i.e. ./Converter.py -a “there’s my friend”. The script will then output comma separated Unicode values.
Of course the ‘ will break the input, and I know I can escape the ‘ by putting a \ in front of it; however, for some reason this stops a comma being added. For example:
./Convert.py "\"hi\""
Outputs:
34104, 105, 34
As you can see, there should be a comma and a space between 34 and 104. This is the code:
def CharCode(text):
print "Unicode values are:",
length = len(text)
for letter in text:
Unicode_values = ord(letter)
sys.stdout.write(str(Unicode_values))
if letter != text[-1]:
sys.stdout.write(', ')
sys.stdout.write('\r\n')
If I remove the
if letter != text[-1]:
This stops the problem happening, but then a comma is added at the very end of the Unicode value string, which I do not want.
My questions thus are a) how can I prevent this happening? and b) is there a better way to take input of this type, would I be better setting up raw_input for instance?
Any help is much appreciated, thanks!
Your condition
says to write a comma and space character whenever the last character is not equal to the value of the last character – in this case, ‘ (your first character) is the same character as ‘ (your last character). You should change the condition to check that it really is the last character, not that the character is the same as the last character. You could do this with
but better might be to use a join method:
Furthermore, if you don’t want to have to use quotes to make your shell send your script your input as one arg, argparse has
nargsoptions (docs) like'+'for gathering as many args as are present. You would still have to escape quotes you want however, and you’d have to make an assumption about the whitespace between these args. (something like' '.join(args)would only approximate your input)