[英]Writing to CSV without tokenizing words into letters
So, I want to take the keys from the dictionary "data" [see code below] and write to a csv file a row of headers, so I'll have two columns, "name" and "language."所以,我想从字典“数据”[见下面的代码]中取出键并将一行标题写入 csv 文件,所以我将有两列,“名称”和“语言”。 Then I want to have four rows, the first being "Dave,Python", the second "Dennis,C" and so on.
然后我想要四行,第一行是“Dave,Python”,第二行是“Dennis,C”,依此类推。 Should look like:
应该看起来像:
name,language
姓名、语言
Dave,Python戴夫,Python
Dennis,C丹尼斯,C
Peter,Java彼得,Java
Jess,Python杰斯,Python
Code:代码:
data = {
'name': ['Dave', 'Dennis', 'Peter', 'Jess'],
'language': ['Python', 'C', 'Java', 'Python']
}
with open("dump.csv", mode="w", newline="") as f:
writer = csv.writer(f, delimiter=",")
writer.writerow(data.keys())
for n, l in zip(data["name"], data["language"]):
writer.writerow(f'{n}{l}')
Here's what I'm actually getting:这是我实际得到的:
name,language
姓名、语言
D,a,v,e,P,y,t,h,o,nD,a,v,e,P,y,t,h,o,n
D,e,n,n,i,s,CD,e,n,n,i,s,C
P,e,t,e,r,J,a,v,aP,e,t,e,r,J,a,v,a
J,e,s,s,P,y,t,h,o,nJ,e,s,s,P,y,t,h,o,n
I'm pretty sure this is happening because strings are indexed in Python and my for n, l [etc.] line is somehow iterating over each index of the strings, but I don't know why it's happening or what to do to fix it.我很确定这种情况正在发生,因为字符串在 Python 中被索引,而我的 for n, l [etc.] 行以某种方式迭代字符串的每个索引,但我不知道为什么会发生这种情况或如何修复它。
Under the hood, strings are just arrays of characters.在引擎盖下,字符串只是字符的 arrays。 Meanwhile,
writerow()
expects an array of values to be turned into a row of fields.同时,
writerow()
期望将一组值转换为一行字段。 Instead, you're passing it a string you've combined together which results in each character being interpreted as a field.相反,您将一个组合在一起的字符串传递给它,这导致每个字符都被解释为一个字段。
You could either recombine n
and l
back into an array您可以将
n
和l
重新组合成一个数组
writer.writerow([n, l])
Or simply don't bother destructuring the enumerated value from the zip()
operation.或者干脆不费心从
zip()
操作中解构枚举值。
for row in zip(data["name"], data["language"]):
writer.writerow(row)
.writerow()
takes an iterable as an argument and writes all the parts as separate values to the csv. .writerow()
将一个可迭代对象作为参数,并将所有部分作为单独的值写入 csv。
You're giving it a string ( f'{n}{l}'
) and that's an iterable of characters, which explains the output you get.你给它一个字符串(
f'{n}{l}'
),这是一个可迭代的字符,这解释了你得到的 output 。
You should do this instead:你应该这样做:
writer.writerow([n, l])
That gives it a list of strings, writing each string to its own position, without breaking it up.这给了它一个字符串列表,将每个字符串写入它自己的 position,而不会分解它。
[Overkill] If you do not mind to add a (unnecessary) package, Pandas can ease the task. [矫枉过正]如果您不介意添加(不必要的)package,Pandas 可以减轻任务。
import pandas as pd
data = {
'name': ['Dave', 'Dennis', 'Peter', 'Jess'],
'language': ['Python', 'C', 'Java', 'Python']
}
df = pd.DataFrame(data)
df.to_csv("dump.csv", index=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.