0

The output of the following code is shown in the attached image:

print(reg_num)
print(final_df)

Image of Ouput ^^^Outputs of print(reg_num)andprint(final_df) as visible on terminal^^^

In the output image, 6 lines of outputs are visible. Pairs of 3 each. I want to save each pair into separate text files.

Expected output:

output1.txt

16SCSE102014
2

output2.txt

16SCSE101350
0

output3.txt

16SCSE101002
0

My attempt:

z = [reg_num, final_df]
print(z)
ctr = 0
for i in z:
  ctr += 1
  with open('d:\\output'+str(ctr)+'.txt', 'w') as f:
     for j in range(len(z)):
     f.write((str(z[j]) + "\n"))

but I couldn't bring the required data into the text files.

What is the solution?

1
  • change the line for j in range(len(z)): to for j in i: Commented Aug 6, 2019 at 10:57

4 Answers 4

1
z = [['2001ABCD2001', '40'],
     ['4002ABCD4002', '30'],
     ['2005WXYZ2005', '20']]

for index, row in enumerate(z, 1):
    dest = "d:\\output{}.txt".format(index)
    with open(dest, 'w') as f:
        f.writelines("{}\n".format(cell) for cell in row)
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer would be way better if you included some explanation in it.
0

This should do the thing. The output files will appear in the same directory you run the python script from.

results = [['2001ABCD2001', '40'],
           ['4002ABCD4002', '30'],
           ['2005WXYZ2005', '20']]

ctr = 0
for res in results:
    ctr += 1
    with open("output{}.txt".format(ctr), 'w') as f:
        f.write("\n".join(res))
        f.write("\n")    # final newline

5 Comments

Its getting the target right, but the text file is being printed character by character in one char per row.
That's weird, it doesn't for me and it actually shouldn't for you either. Did you run this exact snippet?
f.write("\n".join(res)) is returning the error - can only join iterables. So I replaced that with str(results) which is a list and hence an iterable. That results in one character per row output to the respective text files. Also it wrote only two files with same data in each instead of three files with respective data.
res is an iterable in my snippet since it is a list of strings. So I'm asking again, did you run this exact same snippet as a standalone script or did you merge it into your own code? Which version of Python are you using?
I merged your exact snippet. Using Python 3. Can you just run your code on res = ["16SCSCE2001", "2"] and put it into a text? I'll take it from there.
0

Possibly you could just write the elements of each sub-list:

z = [['2001ABCD2001', '40'],
     ['4002ABCD4002', '30'],
     ['2005WXYZ2005', '20']]

ctr = 0
for i in z:
    ctr += 1
    with open('output'+str(ctr)+'.txt', 'w') as f:
        f.write((i[0] + "\n" + i[1]))

Here the text files are written in the same directory where the python script is run from.

1 Comment

My question has changed. I think I misunderstood the list in list concept. Please hae a look again.
-1
a = ['2001ABCD2001', '40']
b = ['4002ABCD4002', '30']

c = "2005WXYZ2005"        
d = "20"

print(a)
print(b)
print(c)
print(d) # 4 final outputs of the main program to be saved to diff files.

z = [a] # my attempt on just 'a'
ctr = 0
for i in z:
    ctr += 1
    with open('output'+str(ctr)+'.txt', 'w') as f: #ctr to increment name of file
        for j in range(len(a)): 
            f.write((str(a[j])+"\n"))

2 Comments

Hi easycodeyeasy, following your snippet I realized a mistake. So I re-edited my question and included your snippet in it. Could you help me out on this? My question is a lot more closer to what I want to get now.
This needs to create 3 txt files.

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.