简体   繁体   中英

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." Then I want to have four rows, the first being "Dave,Python", the second "Dennis,C" and so on. Should look like:

name,language
Dave,Python
Dennis,C
Peter,Java
Jess,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,n
D,e,n,n,i,s,C
P,e,t,e,r,J,a,v,a
J,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.

Under the hood, strings are just arrays of characters. Meanwhile, writerow() expects an array of values to be turned into a row of fields. 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

writer.writerow([n, l])

Or simply don't bother destructuring the enumerated value from the zip() operation.

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.

You're giving it a string ( f'{n}{l}' ) and that's an iterable of characters, which explains the output you get.

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.

[Overkill] If you do not mind to add a (unnecessary) package, Pandas can ease the task.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM