0

I will try to provide example for the question.

Let's say we have 3 lists. e.g :-

list1 =['one','two','three']
list2=['a','b','c']
list3=['mike','jack','ram']

Or, say there are list values for each lines in the file.

['one','two','three']
['a','b','c']
['mike','jack','ram']

Now I want to write the three lists to three different files by creating them. The names of the files should be autogenerated e.g:-

file001.txt
file002.txt
file003.txt
19
  • 3
    Why does simultaneously matter; just open & write one file for each list. Does your real data stream in somehow (wouldn't be a list then)? Is it very large? Commented Feb 21, 2017 at 20:56
  • I am guessing your list values are inside one file?? something like ['one','two','three'] and next line ['a','b','c'] ?? Commented Feb 21, 2017 at 21:08
  • @NickT Yes. I have more than 50 documents that I have parsed into 50 lists. Now I have to write them into 50 files automatically. Commented Feb 21, 2017 at 21:11
  • @everestial007 Each list writes into a diiferent file. Like list1 to file001.txt and list2 into file002.txt and so on.. Commented Feb 21, 2017 at 21:12
  • So, you want to read 50 different files in automated fashion, and each has one line of data. Commented Feb 21, 2017 at 21:12

3 Answers 3

2

I am assuming that your data is in the console and each list is a line.

something like this:

line1 =['one','two','three']
line2=['a','b','c']
line3=['mike','jack','ram']

I merged all the data into one lists of list

all_data = [line1] + [line2] + [line3]

This above part is not necessary if all the list values are line by line in one variable. If not you can merge them using some method.

Now, write each line (list values) to the different file:

count = 1

for data in all_data:
    output = open('file' + str(count) + '.txt', 'w')
    output.write(','.join(data))
    count += 1
    output.close()

This keeps going on until the last value of the list. So, based on how many lists are there. If you want to join the values inside the list you can change the ''.join with something desirable in the single quotes ('').

Hope I helped.

Sign up to request clarification or add additional context in comments.

1 Comment

This answer is close to my solution.
0

You can see a detailed explanation here . But to sum it all up , you define an object of the type file , by opening a file(or creating one if it doesn't exist) , and then writing / reading / etc...

1 Comment

What does that have to do with simultaneity?
0

Use enumerate and string formatting to construct the file names.

s = 'file{:03}.txt'
for n, lyst in enumerate((list1, list2, list3), 1):
    fname = s.format(n)
    with open(fname, 'w') as f:
        #f.write(','.join(lyst))
        f.write('\n'.join(lyst))

If any of the items are not strings change the write to

        f.write('\n'.join(map(str, lyst)))

If the lists are so long that creating a single string to write to the file is prohibitive, change the write to

        for thing in lyst:
            f.write('{}\n'.format(thing))

4 Comments

I think the OP wants to generate as many files as many lines are there. I think the better approach is to read and write each line on the fly.
@everestial007 - when you think a certain method is better, you should say why it is better.
I was just trying to point out that, if data is read and write by each line (on the fly) it will work not matter how many lines are there (50 or millions).
@everestial007 - thnx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.