Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

python - csv.writer writing each character of word in separate column/cell

Objective: To extract the text from the anchor tag inside all lines in models and put it in a csv.

I'm trying this code:

with open('Sprint_data.csv', 'ab') as csvfile:
  spamwriter = csv.writer(csvfile)
  models = soup.find_all('li' , {"class" : "phoneListing"})

  for model in models:

      model_name = unicode(u' '.join(model.a.stripped_strings)).encode('utf8').strip()
      spamwriter.writerow(unicode(u' '.join(model.a.stripped_strings)).encode('utf8').strip())

It's working fine except each cell in the csv contains only one character.

Like this:

|  S  |  A  |   M  |   S  |   U   |  N  |   G   |

Instead of:

|SAMSUNG|

Of course I'm missing something. But what?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

.writerow() requires a sequence ('', (), []) and places each index in it's own column of the row, sequentially. If your desired string is not an item in a sequence, writerow() will iterate over each letter in your string and each will be written to your CSV in a separate cell.

after you import csv

If this is your list:

myList = ['Diamond', 'Sierra', 'Crystal', 'Bridget', 'Chastity', 'Jasmyn', 'Misty', 'Angel', 'Dakota', 'Asia', 'Desiree', 'Monique', 'Tatiana']


listFile = open('Names.csv', 'wb')
writer = csv.writer(listFile)
for item in myList:
    writer.writerow(item)

The above script will produce the following CSV: Names.csv

D,i,a,m,o,n,d
S,i,e,r,r,a
C,r,y,s,t,a,l
B,r,i,d,g,e,t
C,h,a,s,t,i,t,y
J,a,s,m,y,n
M,i,s,t,y
A,n,g,e,l
D,a,k,o,t,a
A,s,i,a
D,e,s,i,r,e,e
M,o,n,i,q,u,e
T,a,t,i,a,n,a

If you want each name in it's own cell, the solution is to simply place your string (item) in a sequence. Here I use square brackets []. :

listFile2 = open('Names2.csv', 'wb')
writer2 = csv.writer(listFile2)
for item in myList:
    writer2.writerow([item])

The script with .writerow([item]) produces the desired results: Names2.csv

Diamond
Sierra
Crystal
Bridget
Chastity
Jasmyn
Misty
Angel
Dakota
Asia
Desiree
Monique
Tatiana

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...