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

  • SEARCH
  • Home
  • 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 8663095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:53:53+00:00 2026-06-12T16:53:53+00:00

I am using argparse to read in arguments for my python code. One of

  • 0

I am using argparse to read in arguments for my python code. One of those inputs is a title of a file [title] which can contain Unicode characters. I have been using 22少女時代22 as a test string.

I need to write the value of the input title to a file, but when I try to convert the string to UTF-8 it always throws an error:

UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0x8f in position 2: ordinal
not in range(128)

I have been looking around and see I need my string to be in the form u"foo" to call .encode() on it.

When I run type() on my input from argparse I see:

<type 'str'>

I am looking to get a response of:

<type 'unicode'>

How can I get it in the right form?

Idea:

Modify argparse to take in a str but store it as a unicode string u"foo":

parser.add_argument(u'title', metavar='T', type=unicode, help='this will be unicode encoded.')

This approach is not working at all. Thoughts?

Edit 1:

Some sample code where title is 22少女時代22:

inputs = vars(parser.parse_args())
title = inputs["title"]
print type(title)
print type(u'foo')
title = title.encode('utf8') # This line throws the error
print title
  • 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-06-12T16:53:54+00:00Added an answer on June 12, 2026 at 4:53 pm

    It looks like your input data is in SJIS encoding (a legacy encoding for Japanese), which produces the byte 0x8f at position 2 in the bytestring:

    >>> '22少女時代22'.encode('sjis')
    b'22\x8f\xad\x8f\x97\x8e\x9e\x91\xe322'
    

    (At Python 3 prompt)

    Now, I’m guessing that to “convert the string to UTF-8”, you used something like

    title.encode('utf8')
    

    The problem is that title is actually a bytestring containing the SJIS-encoded string. Due to a design flaw in Python 2, bytestrings can be directly encoded, and it assumes the bytestring is ASCII-encoded. So what you have is conceptually equivalent to

    title.decode('ascii').encode('utf8')
    

    and of course the decode call fails.

    You should instead explicitly decode from SJIS to a Unicode string, before encoding to UTF-8:

    title.decode('sjis').encode('utf8')
    

    As Mark Tolonen pointed out, you’re probably typing the characters into your console, and it’s your console encoding is a non-Unicode encoding.

    So it turns out your sys.stdin.encoding is cp932, which is Microsoft’s variant of SJIS. For this, use

    title.decode('cp932').encode('utf8')
    

    You really should set your console encoding to the standard UTF-8, but I’m not sure if that’s possible on Windows. If you do, you can skip the decoding/encoding step and just write your input bytestring to the file.

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

Sidebar

Related Questions

I'm using argparse in Python 2.7 for parsing input options. One of my options
I've been using argparse for a Python program that can -process , -upload or
I'm using argparse in python to parse commandline arguments: parser = ArgumentParser() parser.add_argument(--a) parser.add_argument(--b)
I am using python argparse with the following argument definition: parser.add_argument('path', nargs=1, help='File path
I'm using Python 2.6.2 (unfortunately can't upgrade to 2.7, or I'd use argparse). How
Using System.Diagnostics.EventLog .NET type one can programmatically create logs into the Event Viewer application.
I'm trying to pass a list of arguments to a python script using the
Using argparse , is it possible to stop parsing arguments at the first unknown
I am using argparse in my python program and I want to call a
I am using Python's (2.7) argparse facility and would like to automatically sort the

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.