Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6755327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:23:11+00:00 2026-05-26T13:23:11+00:00

I’ve made a quick python script that converts to Unicode from ASCII and back.

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:23:12+00:00Added an answer on May 26, 2026 at 1:23 pm

    Your condition

    if letter != text[-1]: 
    

    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

    for i, letter in enumerate(text):
        #stuff
        if i < len(text) - 1:
            sys.stdout.write(', ')
    

    but better might be to use a join method:

    print u', '.join([str(value) for value in unicode_values])
    

    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 nargs options (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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a bunch of posts stored in text files formatted in yaml/textile (from
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.