1

元々、リストは別のリスト内にネストされていました。リストの各要素は一連の文字列でした。

['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)

テキスト ファイルの最初のリストのみを返します。

aaa664847, Completed, location, mode, 2014-xx-ddT20:00:00.000

テキスト ファイルにすべての結果を含めたい

4

3 に答える 3

1

リストがネストされたリストの場合、次のようにループを使用して行を書き込むことができます。

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()

また、ファイルを閉じることを忘れないでください。

于 2015-02-10T01:58:30.617 に答える
1

resultsリストのリストであると仮定します:

from itertools import chain
outfile = open(fullpath, 'w')
outfile.writelines(chain(*results))

itertools.chainリストを単一のリストに連結します。しかし、writelines改行は書きません。そのために、これを行うことができます:

outfile.write("\n".join(chain(*results))

または、単純に (結果内のすべてのリストに文字列が 1 つしかないと仮定します):

outfile.write("\n".join(i[0] for i in results)
于 2015-02-10T02:00:15.147 に答える
0

これらすべての文字列を 1 つの大きなリストにまとめることができれば、それらをループ処理できます。

コードのどこresultsから来たのかはわかりませんが、これらすべての文字列を単一の大きなリスト (おそらく masterList と呼ばれる) に入れることができれば、次のことができます。

fullpath = ('O:/Location/complete.txt')
outfile = open(fullpath, 'w')

for item in masterList:
    outfile.writelines(item)
于 2015-02-10T01:57:14.830 に答える