Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用换行符连接列表以作为CL参数传递_Python_String_List - Fatal编程技术网

Python 使用换行符连接列表以作为CL参数传递

Python 使用换行符连接列表以作为CL参数传递,python,string,list,Python,String,List,我试图在python中加入一个包含多个字符串的列表,其中一些字符串中包含\t和\n字符以创建字符串 假设我的列表如下所示: ll = ['list', 'of', '\tstrings', '\n\twith', 'newline', '\tcharacters'] ss = ':::'.join(ll) print ss 打印ss,并格式化其\n和\t字符: list:::of::: strings::: with:::newline::: characters\ 而我

我试图在python中加入一个包含多个字符串的列表,其中一些字符串中包含\t和\n字符以创建字符串

假设我的列表如下所示:

ll = ['list', 'of', '\tstrings', '\n\twith', 'newline', '\tcharacters']
ss = ':::'.join(ll)
print ss
打印ss,并格式化其\n和\t字符:

list:::of:::    strings:::

     with:::newline:::  characters\
而我希望它打印的是:

'list:::of:::\tstrings:::\n\twith:::newline:::\tcharacters'

我试过:

ss =[]
for l in ll:
    ss.append(repr(l))
ss = ':::'.join(ll)
它的作用与上面的相同

以及:

接近但打印:

'list':::'of':::'\tstrings':::'\n\twith':::'newline':::'\tcharacters'
我又尝试了几次,但似乎没有一次能满足我的需要

我之所以像上面那样需要它,是因为我正在为脚本编写CL参数,稍后在另一台机器上调用该脚本。将列表作为CL参数传递不起作用,因此我打算将列表作为字符串传递(由“::”)连接),然后在另一张幻灯片上,使用“::”,拆分回列表。当只在原始列表上使用join和split时,这是有效的,但是当我使用硬编码的CL参数调用python脚本时,会抛出一个错误,因为列表转换的字符串参数中有换行符。最后一个示例在传递到第二台机器时有效,但在我将其拆分回时,它给出了以下列表:

["'list'", "'of'", "'\\tstrings'", "'\\n\\twith'", "'newline'", "'\\tcharacters'"]
当我需要:

['list', 'of', '\tstrings', '\n\twith', 'newline', '\tcharacters']
更新:我尝试了下面的所有建议,但仍然没有找到答案,所以现在我只是将列表转储到第一台服务器上的文本文件中,传递文件名而不是列表,然后将文本文件读取到第二台服务器上的列表中。希望此解决方案能够奏效,但我仍在寻找更好的方法的建议。

连接的字符串已经是您想要的了。您可以通过打印其表示来确认这一点:

ll = ['list', 'of', '\tstrings', '\n\twith', 'newline', '\tcharacters']
ss = ':::'.join(ll)
print repr(ss)
# 'list:::of:::\tstrings:::\n\twith:::newline:::\tcharacters'
连接的字符串已按您所需的格式存在。您可以通过打印其表示来确认这一点:

ll = ['list', 'of', '\tstrings', '\n\twith', 'newline', '\tcharacters']
ss = ':::'.join(ll)
print repr(ss)
# 'list:::of:::\tstrings:::\n\twith:::newline:::\tcharacters'

给定第一个代码中的
ss

print ss.encode("string_escape")

(如果可能存在任何非ASCII字符,则可能为“unicode_escape”)

给定第一个代码中的
ss

print ss.encode("string_escape")

(如果可能存在任何非ASCII字符,则可能是“unicode\u escape”)

如果您实际上不希望将制表符和换行符作为参数传递给其他系统,则可能应该使用原始字符串(
r”\n\twith“
)来防止所有代码被解释为转义码


不需要进行没有任何用途的数据转换,您只需撤消即可。

如果您实际上不希望将制表符和换行符作为参数传递给其他系统之外的任何内容,您可能应该使用原始字符串(
r”\n\twith“
)来防止所有代码被解释为转义码


无需进行没有任何用途的数据转换,您只需撤消即可。

谢谢,但是,如果我将repr(ss)作为参数传递,然后尝试将其拆分为另一侧,则会有所帮助:您不需要传递
repr(ss)
,pass
ss
itself我尝试过传递ss,但无法传递ss,因为写入另一端调用python的shell脚本的参数被换行符分隔,这会在使用此参数调用脚本时引发错误。它确实可以传递repr(ss),但是,当我将其拆分回列表时,另一端的结果是:[“'list”,'of','\\t字符串','\\n\\twith','newline',“\\tcharacters'”]谢谢,但是,如果我将repr(ss)作为参数传递,然后尝试在另一边拆分它,这会有所帮助:您不需要传递
repr(ss)
,pass
ss
itself我尝试过传递ss,但无法传递ss,因为写入另一端调用python的shell脚本的参数被换行符分隔,这会在使用此参数调用脚本时引发错误。它确实可以传递repr(ss),但是,当我将其拆分回列表时,另一端的结果是:[“'list”,'of','\\t字符串','\\n\\t字符串','换行符',“\\t字符']