0

here is my problem:

in my csv file, I only have one column and multiple rows containing telephone numbers.

    a 
1  222
2  333
3  444
4  555

what I want is merge them into one string and separate by comma, eg:

   a
1  222,333,444,555

The code I am using right now is:

import csv

b = open('test.csv', 'wb')
a = csv.writer(b)

s = ''
with open ("book2.csv", "rb") as annotate:
    for col in annotate:

        ann = col.lower().split(",")
        s += ann[0] + ','
s = s[:-1] # Remove last comma

a.writerow([s])
b.close()

what I get from this is

   a
1   222,
    333,
    444,
    555

All the numbers are in one cell now (good) but they are not on one line (there is /r/n after each telephone number so I think that's why they are not on one line). Thank you in advance!

2 Answers 2

1
import csv

b = open('test.csv', 'wb')
a = csv.writer(b)

s = ''
with open ("book2.csv", "rb") as annotate:
    for col in annotate:

        ann = col.lower().strip('\n').split(",")
        s += ann[0] + ','
s = s[:-1] # Remove last comma

a.writerow([s])
b.close()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That's what I wanted. So I just need to strip the \n for every item.
0

You're using the csv module, but ignoring csv.reader. It handles all the parsing for you:

#!python2
import csv
with open('book2.csv','rb') as inf, open('test.csv','wb') as outf:
    r = csv.reader(inf)
    w = csv.writer(outf)
    L = [row for row in r] # Read all lines as list of lists.
    L = zip(*L)            # transpose all the lines.
    w.writerows(L)         # Write them all back out.

Input:

222
333
444
555

Output in the .csv file:

222,333,444,555

EDIT: I see now that you want the data in Excel to be in a single cell. The above will put it in a row of four cells:

enter image description here

The following will write a single cell:

#!python2
import csv
with open('book2.csv') as inf, open('test.csv','wb') as outf:
    w = csv.writer(outf)
    data = inf.read().splitlines()
    w.writerow([','.join(data)])

Output in the .csv file:

"222,333,444,555"

Output in Excel:

enter image description here

4 Comments

Hi I am using Python 2, but I get this instead: two rows. First row only has the first telephone number ie 222 second row has the other 3 numbers 333 444 555 but they are not in one cell and not I couldn't see the commas
@Kyle, you copied the code exactly? With the exact input data shown?
Look at the file in notepad not Excel. But I think I understand now that what you really want is "222,333,444,555" so it shows up in a single cell in Excel. Note the quotes.
I tried this with 70k rows of ingredients list and for some reason, they come out different. It's odd. I find 61942 matches of the search "Aqua" in one and 55143 of "Aqua" in the test.csv file...

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.