0

I am currently trying to copy an entire column from a single .csv file to another new .csv file. That sounds rather simple but I keep getting a problem.

import csv
import glob

with open(r"C:\Users\n47-jones\Dropbox\Nevil Programming\test1.csv", "wb") as outfile:
    writer = csv.writer(outfile, delimiter=",")
    for csvfilename in glob.glob(r"C:\Users\n47-jones\Dropbox\Nevil Programming\testdir\*.csv"):
        with open(csvfilename, "rb") as infile:
            reader = csv.reader(infile)
            for row in reader:
                writer.writerow(row[1])

when this is output in the new test1.csv the contents of row[1] is split down into each individual letter.

e.g.

it goes from:

DATE |

to

D | A | T | E

I hope this makes some sense. Thanks for any help.

Nick

1 Answer 1

1

write.writerow() takes a iterable and writes it as a csv row. A string is a iterable that yield its characters.

So your problem is that instead of giving it a list you are giving it a string. If you want the csv to contain only the column you have to give it a iterable with 1 item:

writer.writerow([row[1]])
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you I knew it would be something simple!

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.