tumbleweed
tumbleweed

Reputation: 4630

How to save a new file, the content of a number of lists?

Just a very quick question guys, lets assume that tagged_files is a list of lists:

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.']]

Any idea of how can I save the content of each list in a new single .txt file?. For example, in a new directory:

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

Upvotes: 0

Views: 79

Answers (3)

niemmi
niemmi

Reputation: 17263

Assuming that you want to write all the strings in each list to single line and the target directory exists you can use following code:

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 takes sequence of strings as a parameter and writes them to single line.

Upvotes: 3

Himanshu dua
Himanshu dua

Reputation: 2523

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

Upvotes: 0

Kaeon
Kaeon

Reputation: 11

To write to an external file in python, you first have to open it like this:

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

Then you can write to the file like this:

file.write(yourString)

when you're done, be sure to close the file like this:

file.close()

In your case, assuming your data is stored in a 2-D array, I'd go about it like this:

dataFile.open("Data.txt", 'w')

for file in taggedFiles:

    dataLine = ""

    for data in file:
        dataLine += data + ", "

    dataFile.write(dataLine)

dataFile.close()

Upvotes: 1

Related Questions