Python csv.writer 在单独的列/单元格中写入单词的每个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15129567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
csv.writer writing each character of word in separate column/cell
提问by vivekanon
Objective: To extract the text from the anchor tag inside all lines in modelsand put it in a csv.
目标:从所有行中的锚标记中提取文本models并将其放入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.
除了 csv 中的每个单元格只包含一个字符外,它工作正常。
Like this:
像这样:
| S | A | M | S | U | N | G |
Instead of:
代替:
|SAMSUNG|
Of course I'm missing something. But what?
当然,我错过了一些东西。但是什么?
采纳答案by Eevee
writerowaccepts a sequence. You're giving it a single string, so it's treating that as a sequence, and strings act like sequences of characters.
writerow接受一个序列。你给它一个字符串,所以它把它当作一个序列,而字符串就像字符序列。
What else do you want in this row? Nothing? If so, make it a list of one item:
在这一行你还想要什么?没有?如果是这样,请将其设为包含一项的列表:
spamwriter.writerow([u' '.join(model.a.stripped_strings).encode('utf8').strip()])
(By the way, the unicode()call is completely unnecessary since you're already joining with a unicode delimiter.)
(顺便说一句,unicode()由于您已经加入了 unicode 分隔符,因此该调用完全没有必要。)
回答by mdandr
.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.
.writerow()需要一个序列 ( '', (), []) 并将每个索引按顺序放置在它自己的行列中。如果您想要的字符串不是序列中的一个项目,writerow()则将遍历字符串中的每个字母,并将每个字母写入您的 CSV 文件中的单独单元格中。
after you import csv
您先请 import csv
If this is your list:
如果这是您的清单:
myList = ['Diamond', 'Sierra', 'Crystal', 'Bridget', 'Chastity', 'Jasmyn', 'Misty', 'Angel', 'Dakota', 'Asia', 'Texxxas', 'Desiree', 'Monique', 'Tatiana']
listFile = open('Strippers.csv', 'wb')
writer = csv.writer(listFile)
for item in myList:
writer.writerow(item)
The above script will produce the following CSV: strippers.csv
上述脚本将生成以下 CSV:strippers.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
T,e,x,x,x,a,s
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 []. :
如果您希望每个名称都在它自己的单元格中,解决方案是简单地将字符串 ( item) 放在一个序列中。这里我使用方括号[]。:
listFile2 = open('Strippers2.csv', 'wb')
writer2 = csv.writer(listFile2)
for item in myList:
writer2.writerow([item])
The script with .writerow([item])produces the desired results:
Strippers2.csv
脚本.writerow([item])产生所需的结果:Strippers2.csv
Diamond
Sierra
Crystal
Bridget
Chastity
Jasmyn
Misty
Angel
Dakota
Asia
Texxxas
Desiree
Monique
Tatiana
回答by Reihan_amn
Just surround it with a list sign (i.e [])
只需用列表符号包围它(即[])
writer.writerow([str(one_column_value)])
回答by Steve B
This is usually the solution I use:
这通常是我使用的解决方案:
import csv
with open("output.csv", 'w', newline= '') as output:
wr = csv.writer(output, dialect='excel')
for element in list_of_things:
wr.writerow([element])
output.close()
This should provide you with an output of all your list elements in a single column rather than a single row.
这应该会在单列而不是单行中为您提供所有列表元素的输出。
Key points here is to iterate over the list and use '[list]' to avoid the csvwriter sequencing issues.
这里的关键点是遍历列表并使用“[list]”来避免 csvwriter 排序问题。
Hope this is of use!
希望这是有用的!

