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 894813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:27:28+00:00 2026-05-15T14:27:28+00:00

I am calling a python script with the following command line: myscript.py –myopt=[(5.,5.),(-5.,-5.)] The

  • 0

I am calling a python script with the following command line:

myscript.py --myopt="[(5.,5.),(-5.,-5.)]"

The question is — how to convert myopt to a list variable. My solution was to use optparse, treating myopt as a string, and using

(options, args) = parser.parse_args()    
myopt = eval(options.myopt)

Now, because I used eval() I feel a bit like Dobby the house elf, having knowingly transgressed the commandments of great (coding) wizards, and wanting to self-flagellate myself in punishment.

But are there better options for parsing lists or tuples or lists of tuples from the command line?.. I’ve seen solutions that use split(), but this won’t work here since this isn’t a simple list. Keep in mind, also, that this is being done in the context of mostly one-off scientific computing with no security concerns — so perhaps eval() isn’t as evil here?..

  • 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-15T14:27:28+00:00Added an answer on May 15, 2026 at 2:27 pm

    ast.literal_eval(node_or_string):

    Safely evaluate an expression node or
    a string containing a Python
    expression. The string or node
    provided may only consist of the
    following Python literal structures:
    strings, numbers, tuples, lists,
    dicts, booleans, and None.

    This can be used for safely evaluating
    strings containing Python expressions
    from untrusted sources without the
    need to parse the values oneself.

    So you can do

    import ast
    (options, args) = parser.parse_args()    
    myopt = ast.literal_eval(options.myopt)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm calling a python script from the below one using subprocess. From the command
I'm calling a java program from my python code in the following way: subprocess.check_output([java,
I am calling a python script from PHP. The python program has to return
Right, I am calling a Python script in a PHP script. This PHP script
I have following script: #!/usr/bin/python while True: x = raw_input() print x[::-1] I am
I'm calling a python script (B) from another python script (A). Using subprocess.call, how
Under Windows, executing the following Python script I get an ERRORLEVEL of 0 instead
I am calling an external program within Python script using subprocess. The external program
How does one acquire the directory of the Windows Shortcut calling a python script.
If I invoke an executable script: $ ./myscript which has the following defintion at

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.