0

I've two csv files to append & create a new csv file with the result. If I use 'binary' like 'rb' or 'wb' to read the write then it's throwing

_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

If I use only 'r' & 'w' to read & write the following code giving me error like

IndexError: list index out of range

with open('file1.csv','rt') as f, open('final.csv','wt')as out:
        first=csv.reader(f)
        final = csv.writer(out)
        for row in first:
                result=row[1]
                final.writerow(result)

with open('file21.csv','rt') as s, open('final.csv','wt')as out:
        second =  csv.reader(s)
        final = csv.writer(out)
        for row in second:
                result=row[1]
                final.writerow(result)
5
  • Could you provide the whole stacktrace? Commented Feb 25, 2016 at 9:01
  • Traceback (most recent call last): File "fileAppends.py", line 18, in <module> result=row[0] IndexError: list index out of range @zsquare Commented Feb 25, 2016 at 9:16
  • What are you trying to accomplish? Commented Feb 25, 2016 at 9:18
  • Join two csv files that's it. But both csv file has same header so I can copy headers from both. @Gijs Commented Feb 25, 2016 at 9:31
  • Use this answer: stackoverflow.com/questions/35611976/…. Commented Feb 25, 2016 at 9:36

1 Answer 1

0

You have a row with no two values. Try row[0], and check if you need to specify a delimiter.

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

3 Comments

Traceback (most recent call last): File "fileAppends.py", line 18, in <module> result=row[0] IndexError: list index out of range sorry now showing this
You should show a part of your files, otherwise it's going to be hard to find your problem.
Two csv files with the same header/titles on the columns but different in values. Okay um trying to provide examples. @Gijs

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.