Why Does CSVwriter.Writerow() Put a Comma After Each Character

Why does csvwriter.writerow() put a comma after each character?

It expects a sequence (eg: a list or tuple) of strings. You're giving it a single string. A string happens to be a sequence of strings too, but it's a sequence of 1 character strings, which isn't what you want.

If you just want one string per row you could do something like this:

csvwriter.writerow([JD])

This wraps JD (a string) with a list.

Python csv writer outputs commas after each letter

The writerow method expects an iterable consisting of all the items of the row. As you are passing it a string, it will iterate over each character and consider each as an item.

To instead go over the fields, instead of

business_data = business_name+","+street+","+city+state+","+zip+","+telephone
all_data.append(business_data)

You may want to try something like

all_data.append([business_name, street, city + state, zip, telephone])

Output CSV has comma after every character in python

Try this. Your code is treating the string as a list

writer.writerow(["Form Action"])
writer.writerow(["Form URL"])
writer.writerow(["Suspected Request"])

CSV Comma after every character python

Replace writerows with writerow.

The problem is that writerows expects a list of lists, where each inward list is a row to write. Since you're giving it a list of strings, it thinks every character in the string is a column in the csv file.

Note that you're also not using id in your second for loop, and you are opening a file named outfile1 for reading, so you may need to rethink your names here. I would guess you want to replace the 0s in your second for loop with id, but I'm not sure.

My CSV writer code writes separators between characters and not strings

The problem is here:

line=','.join(r)
print(line)
write.writerow(line)

The writerow method wants a list of columns. It will add the commas between the columns (and quote or escape anything that needs it, etc.).

But you're not giving it a list of columns; you're giving it a single string. That's what ','.join(r) does: turns a list of columns into a single comma-separated string.

When you give writerow a string, instead of a list of strings, it treats the string as a sequence of characters. (That's not specific to csv; in Python, a string is a sequence of characters.) So it treats each character as a column, and adds commas between them.

Just do this:

write.writerow(r)

CSV.writerow has comma between every character?

You are passing a string to the csv.write, which it then interprets as a list and therefore splits it by each list element, i.e. character. I've made this mistake so many times...

Try this:

# add coustom code to split the row up into the values, hint user row.split()
csvout.writerow([row, output])

csv.writer inserting comma between each character

The writerows method that you're calling expects its argument to be an iterable containing rows. Each row should be iterable itself, with its values being the items in the row. In your case, the values are strings, which can be iterated upon to give their characters. Unfortunately, that's not what you intended!

Exactly how to fix this issue depends on what you want the output to be.

If you want your output to consist of a single row, then just change your call to writerow to instead of writerows (note the plural). The writerow method will only write a single row out, rather than trying to write several of them at once.

On the other hand, if you want many rows, with just one item in each one (forming a single column), then you'll need to transform your data a little bit. Rather than directly passing in your list of strings, you need to produce an iterable of rows with one item in them (perhaps 1-tuples). Try something like this:

wr.writerows((item,) for item in plugara)

This call uses a generator expression to transform each string from plugara into a 1-tuple containing the string. This should produce the output you want.

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

writerow accepts a sequence. You're giving it a single string, so it's treating that as a sequence, and strings act like sequences of characters.

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.)



Related Topics



Leave a reply



Submit