my_list=[('127.0.0.1', '-', '-', '06/Apr/2017:00:00:00 -0400',
'POST /Apps/js_recommanded_jobs.php HTTP/1.1',
'200', '20546', '-', '-'),
('127.0.0.1', '-', '-', '06/Apr/2017:00:00:00 -0400',
'POST /Apps/auto_suggestion_solr.php HTTP/1.1',
'200', '185', '-', '-')]
我需要输出如下:
('127.0.0.1', '-', '-', '06/Apr/2017:00:00:00 -0400', 'POST /Apps/js_recommanded_jobs.php HTTP/1.1', '200', '20546', '-', '-'),
('127.0.0.1', '-', '-', '06/Apr/2017:00:00:00 -0400', 'POST /Apps/auto_suggestion_solr.php HTTP/1.1', '200', '185', '-', '-')
答案 0 :(得分:1)
如果您需要将其保存为字符串,则可以执行以下操作。
编写由分隔符分隔的每个元组',\ n'到sample_file.txt
with open('sample_file.txt', 'wb') as new_file:
new_list = map(str, my_list)
new_file.write(",\n".join(new_list))
希望它有所帮助!
答案 1 :(得分:0)
<强>演示:强>
>>> fp = open("myfile.txt", "w")
>>> for row in my_list:
... fp.write(str(row) + "\n")
...
>>> fp.close()
>>>
编辑2: 使用map和string join方法删除最后不必要的逗号。
<强>演示:强>
>>> fp = open("myfile.txt", "w")
>>> fp.write(",\n".join(map(str, my_list)))
>>> fp.close()
答案 2 :(得分:0)
此代码有效。
with open('out.txt', 'w') as f:
for line in my_list:
f.write(str(line)+'\n')
答案 3 :(得分:0)
这将完成这项工作。
with open('mylist.txt', 'w') as f:
my_list = [('a','b'),('b','c')]
listtocopy = ',\n'.join(str(x) for x in my_list)
f.write(listtocopy)
f.close()