如何保存新文件,多个列表的内容?

时间:2016-04-18 05:33:08

标签: python list python-2.7 io

只是一个非常快速的问题,让我们假设tagged_files是一个列表列表:

tagged_files = [[u'I\tPP\tI', u'am\tVBP\tbe', u'an\tDT\tan', u'amateur\tJJ\tamateur', u'photographer\tNN\tphotographer', u'and\tCC\tand', u'own\tJJ\town', u'three\tCD\tthree', u'DSLR\tNP\tDSLR', u'cameras\tNNS\tcamera', u'with\tIN\twith', u'a\tDT\ta', u'selection\tNN\tselection', u'of\tIN\tof', u'lenses\tNNS\tlens', u'.\tSENT\t.']
    [u'a\tDT\ta', u'good\tJJ\tgood', u'price\tNN\tprice', u'!\tSENT\t!']
    [u',\t,\t,', u'but\tCC\tbut', u'at\tIN\tat', u'the\tDT\tthe', u'least\tJJS\tleast', u'I\tPP\tI', u'can\tMD\tcan', u'say\tVV\tsay', u'that\tIN/that\tthat', u'finding\tVVG\tfind', u'them\tPP\tthem', u'was\tVBD\tbe', u'unintuitive\tJJ\tunintuitive', u'for\tIN\tfor', u'me\tPP\tme', u'.\tSENT\t.']]

如何知道如何在新的单个.txt文件中保存每个列表的内容?例如,在新目录中:

new_directory
      --->sub_list1.txt
      --->sub_list2.txt
      --->sub_list3.txt

3 个答案:

答案 0 :(得分:3)

假设您要将每个列表中的所有字符串写入单行并且目标目录存在,您可以使用以下代码:

tagged_files = [
    [u'I\tPP\tI', u'am\tVBP\tbe', u'an\tDT\tan', u'amateur\tJJ\tamateur', u'photographer\tNN\tphotographer', u'and\tCC\tand', u'own\tJJ\town', u'three\tCD\tthree', u'DSLR\tNP\tDSLR', u'cameras\tNNS\tcamera', u'with\tIN\twith', u'a\tDT\ta', u'selection\tNN\tselection', u'of\tIN\tof', u'lenses\tNNS\tlens', u'.\tSENT\t.'],
    [u'a\tDT\ta', u'good\tJJ\tgood', u'price\tNN\tprice', u'!\tSENT\t!'],
    [u',\t,\t,', u'but\tCC\tbut', u'at\tIN\tat', u'the\tDT\tthe', u'least\tJJS\tleast', u'I\tPP\tI', u'can\tMD\tcan', u'say\tVV\tsay', u'that\tIN/that\tthat', u'finding\tVVG\tfind', u'them\tPP\tthem', u'was\tVBD\tbe', u'unintuitive\tJJ\tunintuitive', u'for\tIN\tfor', u'me\tPP\tme', u'.\tSENT\t.']
]

for i, l in enumerate(tagged_files, 1):
    with open('new_directory/sub_list{0}.txt'.format(i), 'w') as f:
        f.writelines(l)

writelines将字符串序列作为参数并将它们写入单行。

答案 1 :(得分:1)

要在python中写入外部文件,首先必须打开它:

SELECT
    ds.master_rcpt_gen,
    ds.visa_rcpt_gen,
    dsp.curr_amount,
    dsp.curr_id,
    mcr.conv_rate,
    mcr.curr_code,
    mcr.curr_desc,
    mcr.curr_type,
    (conv_rate / (select mcr.conv_rate from mst_currencies_rate mcr where mcr.curr_code = 'USD')) conv_ratio,
    curr_amount * (conv_rate / (select mcr.conv_rate from mst_currencies_rate mcr where mcr.curr_code = 'USD')) conv_usd,
    (select mtr.USD_tax_rate from mst_tax_rate mtr) * (curr_amount * (conv_rate / (select mcr.conv_rate from mst_currencies_rate mcr where mcr.curr_code = 'USD'))) AS conv_taxrate_commission,
    CASE WHEN mcr.curr_type = 'LOCAL' THEN conv_taxrate_commission = dsp.curr_amount ELSE (select mtr.USD_tax_rate from mst_tax_rate mtr) * (curr_amount * (conv_rate / (select mcr.conv_rate from mst_currencies_rate mcr where mcr.curr_code = 'USD')))
    END final_conv_rate 
FROM
    dtl_sr ds
INNER JOIN dtl_sr_payment dsp ON dsp.flight_id = ds.flight_id
AND dsp.sr_id = ds.SR_ID
INNER JOIN mst_currencies_rate mcr ON mcr.curr_id = dsp.curr_id 

然后你可以写这样的文件:

file = open("Your_File.txt", 'w')

完成后,请务必关闭此文件:

file.write(yourString)

在您的情况下,假设您的数据存储在二维数组中,我会这样做:

file.close()

答案 2 :(得分:0)

count=0
for lst in tagged_files:
  count=count+1
  f = open('output'+str(count)+'.txt', 'w')
  f.writelines(( "%s\n" % item for item in lst ) )