将多个列表写入 python 中的文本文件 - 2.7
Write multiple lists to text file in python - 2.7
最初列表嵌套在另一个列表中。列表中的每个元素都是一系列字符串。
['aaa664847', 'Completed', 'location', 'mode', '2014-xx-ddT20:00:00.000']
我加入了列表中的字符串,然后追加到结果中。
results.append[orginal]
print results
['aaa664847, Completed, location, mode, 2014-xx-ddT20:00:00.000']
['aaa665487, Completed, location, mode, 2014-xx-ddT19:00:00.000']
['aaa661965, Completed, location, mode, 2014-xx-ddT18:00:00.000']
['aaa669696, Completed, location, mode, 2014-xx-ddT17:00:00.000']
['aaa665376, Completed, location, mode, 2014-xx-ddT16:00:00.000']
我希望将每个列表写入一个文本文件。列表的数量可以变化。
我当前的代码:
fullpath = ('O:/Location/complete.txt')
outfile = open(fullpath, 'w')
outfile.writelines(results)
returns 仅文本文件中的第一个列表:
aaa664847, Completed, location, mode, 2014-xx-ddT20:00:00.000
我希望文本文件包含所有结果
如果您可以将所有这些字符串收集到一个大列表中,则可以遍历它们。
我不确定 results
来自你的代码,但如果你可以将所有这些字符串放在一个大列表(可能称为 masterList)中,那么你可以这样做:
fullpath = ('O:/Location/complete.txt')
outfile = open(fullpath, 'w')
for item in masterList:
outfile.writelines(item)
如果你的列表是嵌套列表,你可以使用循环来写行,像这样:
fullpath = ('./data.txt')
outfile = open(fullpath, 'w')
results = [['aaa664847, Completed, location, mode, 2014-xx-ddT20:00:00.000'],
['aaa665487, Completed, location, mode, 2014-xx-ddT19:00:00.000'],
['aaa661965, Completed, location, mode, 2014-xx-ddT18:00:00.000'],
['aaa669696, Completed, location, mode, 2014-xx-ddT17:00:00.000'],
['aaa665376, Completed, location, mode, 2014-xx-ddT16:00:00.000']]
for result in results:
outfile.writelines(result)
outfile.write('\n')
outfile.close()
此外,记得关闭文件。
假设results
是一个列表列表:
from itertools import chain
outfile = open(fullpath, 'w')
outfile.writelines(chain(*results))
itertools.chain
会将列表连接成一个列表。
但是 writelines
不会写换行符。为此,您可以这样做:
outfile.write("\n".join(chain(*results))
或者,说白了(假设结果中的所有列表只有一个字符串):
outfile.write("\n".join(i[0] for i in results)
最初列表嵌套在另一个列表中。列表中的每个元素都是一系列字符串。
['aaa664847', 'Completed', 'location', 'mode', '2014-xx-ddT20:00:00.000']
我加入了列表中的字符串,然后追加到结果中。
results.append[orginal]
print results
['aaa664847, Completed, location, mode, 2014-xx-ddT20:00:00.000']
['aaa665487, Completed, location, mode, 2014-xx-ddT19:00:00.000']
['aaa661965, Completed, location, mode, 2014-xx-ddT18:00:00.000']
['aaa669696, Completed, location, mode, 2014-xx-ddT17:00:00.000']
['aaa665376, Completed, location, mode, 2014-xx-ddT16:00:00.000']
我希望将每个列表写入一个文本文件。列表的数量可以变化。
我当前的代码:
fullpath = ('O:/Location/complete.txt')
outfile = open(fullpath, 'w')
outfile.writelines(results)
returns 仅文本文件中的第一个列表:
aaa664847, Completed, location, mode, 2014-xx-ddT20:00:00.000
我希望文本文件包含所有结果
如果您可以将所有这些字符串收集到一个大列表中,则可以遍历它们。
我不确定 results
来自你的代码,但如果你可以将所有这些字符串放在一个大列表(可能称为 masterList)中,那么你可以这样做:
fullpath = ('O:/Location/complete.txt')
outfile = open(fullpath, 'w')
for item in masterList:
outfile.writelines(item)
如果你的列表是嵌套列表,你可以使用循环来写行,像这样:
fullpath = ('./data.txt')
outfile = open(fullpath, 'w')
results = [['aaa664847, Completed, location, mode, 2014-xx-ddT20:00:00.000'],
['aaa665487, Completed, location, mode, 2014-xx-ddT19:00:00.000'],
['aaa661965, Completed, location, mode, 2014-xx-ddT18:00:00.000'],
['aaa669696, Completed, location, mode, 2014-xx-ddT17:00:00.000'],
['aaa665376, Completed, location, mode, 2014-xx-ddT16:00:00.000']]
for result in results:
outfile.writelines(result)
outfile.write('\n')
outfile.close()
此外,记得关闭文件。
假设results
是一个列表列表:
from itertools import chain
outfile = open(fullpath, 'w')
outfile.writelines(chain(*results))
itertools.chain
会将列表连接成一个列表。
但是 writelines
不会写换行符。为此,您可以这样做:
outfile.write("\n".join(chain(*results))
或者,说白了(假设结果中的所有列表只有一个字符串):
outfile.write("\n".join(i[0] for i in results)