Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/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 2.7在一行上打印列表不起作用_Python_Python 2.7 - Fatal编程技术网

Python 2.7在一行上打印列表不起作用

Python 2.7在一行上打印列表不起作用,python,python-2.7,Python,Python 2.7,我已经尝试了这一切和其他一些谷歌搜索,但我似乎无法让它工作 注*我查阅了有关打印的帮助,以测试outfile.write的输出,但在测试中仍然无法正确格式化 我的代码如下所示: def ipfile(): global ip_targets ip_target_file = raw_input('Place target file location here: ') try: ip_holder = open(ip_target_file,'r') except

我已经尝试了这一切和其他一些谷歌搜索,但我似乎无法让它工作

注*我查阅了有关打印的帮助,以测试
outfile.write
的输出,但在测试中仍然无法正确格式化

我的代码如下所示:

def ipfile():
    global ip_targets
    ip_target_file = raw_input('Place target file location here: ')
    try: ip_holder = open(ip_target_file,'r')
    except IOError:
        os.system('clear')
        print('Either I can\'t read that file, or it\'s not a file')
        os.system('sleep 3')
        return
    ip_targets = ip_holder.readlines()

def inbound_connections():   
    i = 0
    uri_ip_src = ''
    uri_ip_dst = ''
    while i < (len(ip_targets)):
        uri_ip_src_repeater = ('ip.src%3D%3D' + ip_targets[i])
        uri_ip_src = uri_ip_src + '||' + uri_ip_src_repeater
        uri_ip_dst_repeater = ('ip.dst%3D%3D' + ip_targets[i])
        uri_ip_dst = uri_ip_dst + '||' + uri_ip_dst_repeater
        i += 1          
    url = '&expression=' + "(" + (uri_ip_src[2:]) + ")" + "%26(" + (uri_ip_dst[2:]) + ")"
    #Write output to file functions
    outfile=open("jacobi_queries.txt","a")
    outfile.write("Same -> Same Connections " + str(datetime.datetime.now())[:16])
    outfile.write("\n")
    outfile.write(call_moloch + "/sessions?" + timestamp + url + "' 2>/dev/null &")
    outfile.write("\n")
    outfile.close()
    
你的问题是:

ip_targets = ip_holder.readlines()
在每行末尾都有一个换行符。在将该字符添加到
uri\u ip\u src\u repeater
之前,您希望从每个元素的末尾删除该字符。这应该起作用:

ip_targets = [i.rstrip('\n') for i in ip_targets]
但您也可以从源代码中清晰地抓取它们,而不必在以下情况下删除不需要的字符:

ip_targets = ip_holder.read().splitlines() 

哦,忘了提到我的文本文件是一个以返回为分隔的列表,这需要保留。我不明白…这里的
print
在哪里起作用??请阅读并编辑相应的问题。滚动至代码底部。Outfile.write是我在Python 2中遇到的问题(我假设它的工作原理与print类似),
print
是一条语句,它与
file.write
完全不同。你展示的
write
调用中没有一个会产生任何你有问题的输出,所以这个问题真的没有任何意义。我不明白这是怎么回事,你给了我反馈来修复我的帖子,但后来它被否决了,所以现在即使它被修复了也不会被看到。我错过了一些东西,但现在我的问题无法得到解决。交易内容是什么?但第二部分“ip_targets=[i.rstrip('\n')for i in ip_targets]”在“ip_targets=ip_holder.readlines()”之后添加了这一点,效果非常好。谢谢你的帮助!我很想去,但由于被上面的人投票否决而被遗忘,我没有能力投票支持你的答案。
ip_targets = ip_holder.read().splitlines()