1

I am having problems getting my csv file to format correctly when merging it with another. I have the following code:

list_dir = glob.glob('C:/.../*')
imprint = 'C:/.../imprint_report.csv'
export_dir = 'C:/.../Export//'

for imprint_d in csv.DictReader(open(imprint)):
    for list_file in list_dir:
        list_token = os.path.basename(list_file)[:6]
        if imprint_d['token'] == list_token:
            v = open(list_file, 'rb')
            r = csv.reader(v, delimiter='\t')
            row0 = r.next()
            row0.append('token')
            all_rows = []
            for item in r:
                item.append(imprint_d['token'])
                all_rows.append(item)
                #print item
            output = open(export_dir+list_token+'_append.csv', 'wb')
            writer = csv.writer(output, lineterminator=',')
            writer.writerows(all_rows)

Which successfully merges the data I need, however the formatting is incorrect. Rather than have the appended data be set to each row and maintaining the format of list_file, where x is the data I am appending:

col1    col2    col3    col4
  a       b       c       x
  d       e       f       x
  g       h       i       x

I get all data being merged into one row like so:

a    b    c    x    d    e    f    x    g    h    i    x

Any thoughts on where I am going wrong here? Thanks!

2
  • 2
    Change lineterminator=',' maybe? Commented Nov 12, 2013 at 19:47
  • @fp That did it, changed lineterminator=',' to lineterminator='\n' and now I'm good. Make that into an answer so I can accept it and close this question out. Thanks! Commented Nov 12, 2013 at 19:50

1 Answer 1

2

Change lineterminator=',' maybe?

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

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.