0

I am trying to append several lists each from a different csv files in Python. Here is the code I am using:

from_ls = [1,2,3,4,5]
to_ls = [2,3,4,5,6]
mylists = []
for i in range(len(from_ls)):
    from_ = from_ls[i]
    to_ = to_ls[i]
    print(str(from_)+'-'+str(to_))
    f = 'data'+str(from_)+'to'+str(to_)+'.csv'
    if os.path.exists(f):
        with open('data'+str(from_)+'to'+str(to_)+'.csv') as f:
            reader = csv.reader(f)
            mylists.extend(reader)
        print('file '+f+' append successfully')
    else:
        print('file '+f+' not found')

It gives me the following error:

1-2
Traceback (most recent call last):
  File "<stdin>", line 12, in <module>
TypeError: can only concatenate str (not "_io.TextIOWrapper") to str

I know that f = 'data'+str(from_)+'to'+str(to_)+'.csv' creates an _io.TextIOWrapper but I don't know how to turn it into a string so that I can read the file.

I have tried to read the csv files individually as below and it works fine:

i=0
from_ = from_ls[i]
to_ = to_ls[i]
with open('data'+str(from_)+'to'+str(to_)+'.csv') as f:
    reader = csv.reader(f)
    mylists = list(reader)
2
  • What is text? Commented Jul 3, 2020 at 18:39
  • I have corrected: it is actually mylists Commented Jul 3, 2020 at 18:44

2 Answers 2

1

I've tried to reproduce your error, but I couldn't. However I've found some mistakes.

import os
import csv

from_ls = [1,2,3,4,5]
to_ls = [2,3,4,5,6]
mylists = []
for i in range(len(from_ls)):
    from_ = from_ls[i]
    to_ = to_ls[i]
    print(str(from_)+'-'+str(to_))
    fname = 'data'+str(from_)+'to'+str(to_)+'.csv'
    if os.path.exists(fname):
        with open(fname) as f:
            reader = csv.reader(f)
            mylists.extend(reader)
        print('file '+fname+' append successfully')
    else:
        print('file '+fname+' not found')

I do not have csv files here. Try to see if it corrects your code.

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

Comments

0

It's here:

    print('file '+f+' append successfully')
else:
    print('file '+f+' not found')

f is the variable you assigned open('data'+str(from_)+'to'+str(to_)+'.csv') to.

Although you have f = 'data'+str(from_)+'to'+str(to_)+'.csv', you have overwritten its value later in the code. Simply change the name of one of your variables.

Corrected version:

from_ls = [1,2,3,4,5]
to_ls = [2,3,4,5,6]
mylists = []
for i in range(len(from_ls)):
    from_ = from_ls[i]
    to_ = to_ls[i]
    print(str(from_)+'-'+str(to_))
    fn = 'data'+str(from_)+'to'+str(to_)+'.csv' # Note the change of variable name
    if os.path.exists(fn): # And here
        with open('data'+str(from_)+'to'+str(to_)+'.csv') as f:
            reader = csv.reader(f)
            mylists.extend(reader)
        print('file '+fn+' append successfully') # Here
    else:
        print('file '+fn+' not found') # Here also

Comments

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.