将字符串转换为列表 [英] convert string to list

查看:340
本文介绍了将字符串转换为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我是Python的真正初学者,我想知道是否有办法将
转换成一个字符串(其中包含一个列表) )到真实的清单。


我需要这个,因为我想从bash中给出一个列表作为我的

Python脚本的参数:


#!/ usr / bin / python

import sys

argument1 = sys.argv [1]

thelist = ???(argument1)


然后像这样从bash调用脚本


thescript.py [" A"," ; B"," C"]


谢谢你,

Jochen

Hi,

I am a real beginner in Python and I was wondering if there is a way to
convert a string (which contains a list) to a "real" list.

I need this since I would like to give a list as an argument to my
Python script from the bash:

#!/usr/bin/python
import sys
argument1 = sys.argv[1]
thelist = ???(argument1)

and then call the script from the bash like this

thescript.py ["A","B","C"]

Thanks you,
Jochen

推荐答案

Jochen Hub写道:
Jochen Hub wrote:

我需要这个,因为我想从bash中列出一个列表作为我的
Python脚本的参数:

#!/ usr / bin / python
import sys
argument1 = sys.argv [1]
thelist = ???(argument1)

然后像这样从bash调用脚本

thescript.py [" A"," B"," C"]

I need this since I would like to give a list as an argument to my
Python script from the bash:

#!/usr/bin/python
import sys
argument1 = sys.argv[1]
thelist = ???(argument1)

and then call the script from the bash like this

thescript.py ["A","B","C"]




为什么不给你独立的args


thescript.pyA " B" C


并执行此操作:


thelist = sys.argv [1:]


-

问候,


Diez B. Roggisch



Why don''t you give the args standalone

thescript.py "A" "B" "C"

and do this:

thelist = sys.argv[1:]

--
Regards,

Diez B. Roggisch


2004年3月-10-13在14:03 +0200,Jochen Hub写道:
On Wed, 2004-10-13 at 14:03 +0200, Jochen Hub wrote:


我是Python的真正初学者,我想知道是否有办法
将字符串(包含列表)转换为真实字符串。清单。

我需要这个,因为我想从bash中给出一个列表作为我的
Python脚本的参数:

#!/ usr / bin / python
import sys
argument1 = sys.argv [1]
thelist = ???(argument1)

然后从bash这样调用脚本就像这样

thescript.py [" A,B,C]
Hi,

I am a real beginner in Python and I was wondering if there is a way to
convert a string (which contains a list) to a "real" list.

I need this since I would like to give a list as an argument to my
Python script from the bash:

#!/usr/bin/python
import sys
argument1 = sys.argv[1]
thelist = ???(argument1)

and then call the script from the bash like this

thescript.py ["A","B","C"]




您可以使用eval。更好的是,你可以简单地将你的列表作为bash的一系列参数传递给你:


#!/ bin / bash

myscript.py ABC

#!/ usr / bin / python

import sys

thelist = sys.argv [1:]


问候,

悬崖


-

Cliff Wells< cl *** *********@comcast.net>



You can use eval for this. Better, you could simply pass your list as a
series of arguments from bash:

#!/bin/bash
myscript.py A B C
#!/usr/bin/python
import sys
thelist = sys.argv[1:]

Regards,
Cliff

--
Cliff Wells <cl************@comcast.net>


>>
thescript.py [" A"," B"," C"]

您可以使用eval。更好的是,您可以简单地将您的列表作为一系列参数从bash传递出来:
thescript.py ["A","B","C"]

You can use eval for this. Better, you could simply pass your list as a
series of arguments from bash:




我尝试使用eval ...只需使用


thelist = eval(argv [1])


它不起作用:-(,我收到错误消息

文件" ./ findCenter.py",第64行,在?

print eval(sys.argv [1])

文件"< ; string>",第0行,在?

NameError:name''A''未定义

#!/ bin / bash
myscript。 py ABC



I tried with eval...simply using

thelist=eval(argv[1])

it didn''t work :-(, I got the error message

File "./findCenter.py", line 64, in ?
print eval(sys.argv[1])
File "<string>", line 0, in ?
NameError: name ''A'' is not defined

#!/bin/bash
myscript.py A B C




问题是我想将一个或两个列表传递给脚本,所以它b / b
应该可以区分


thescript [A,B] [C,D,E]

thescript [" A,B,C[D,E]

thescript [A,B,C,D ;,E"]



The problem is that I want to pass one or two lists to the script, so it
should be able to distinguish between

thescript ["A","B"] ["C","D",E"]
thescript ["A","B","C"] ["D",E"]
thescript ["A","B","C","D",E"]


这篇关于将字符串转换为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆