简体   繁体   English

为什么 csvwriter.writerow() 在每个字符后加一个逗号?

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

This code opens the URL and appends the /names at the end and opens the page and prints the string to test1.csv :此代码打开 URL 并在末尾附加/names并打开页面并将字符串打印到test1.csv

import urllib2
import re
import csv

url = ("http://www.example.com")
bios = [u'/name1', u'/name2', u'/name3']
csvwriter = csv.writer(open("/test1.csv", "a"))

for l in bios:
    OpenThisLink = url + l
    response = urllib2.urlopen(OpenThisLink)
    html = response.read()
    item = re.search('(JD)(.*?)(\d+)', html)
    if item:
        JD = item.group()
        csvwriter.writerow(JD)
    else:
        NoJD = "NoJD"
        csvwriter.writerow(NoJD)

But I get this result:但我得到这个结果:

J,D,",", ,C,o,l,u,m,b,i,a, ,L,a,w, ,S,c,h,o,o,l,....

If I change the string to ("JD", "Columbia Law School"....) then I get如果我将字符串更改为 ("JD", "Columbia Law School"...) 然后我得到

JD, Columbia Law School...)

I couldn't find in the documentation how to specify the delimeter.我在文档中找不到如何指定分隔符。

If I try to use delimeter I get this error:如果我尝试使用delimeter ,我会收到此错误:

TypeError: 'delimeter' is an invalid keyword argument for this function

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. 一个字符串也恰好是一个字符串序列,但是它是一个由1个字符串组成的序列,这不是您想要的。

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. 这会用列表包装JD(字符串)。

The csv.writer class takes an iterable as it's argument to writerow; csv.writer类使用一个可迭代的变量作为writerow的参数。 as strings in Python are iterable by character, they are an acceptable argument to writerow, but you get the above output. 由于Python中的字符串可以按字符进行迭代,因此它们是writerow可接受的参数,但是您会得到上面的输出。

To correct this, you could split the value based on whitespace (I'm assuming that's what you want) 为了解决这个问题,您可以根据空格分割值(我假设这就是您想要的)

csvwriter.writerow(JD.split())

This happens, because when group() method of a MatchObject instance returns only a single value, it returns it as a string. 发生这种情况的原因是,当MatchObject实例的group()方法仅返回单个值时,它将作为字符串返回。 When there are multiple values, they are returned as a tuple of strings. 当有多个值时,它们将作为字符串元组返回。

If you are writing a row, I guess, csv.writer iterates over the object you pass to it. 如果您要写一行,我想csv.writer会遍历传递给它的对象。 If you pass a single string (which is an iterable), it iterates over its characters, producing the result you are observing. 如果传递单个字符串(可迭代),则会对其字符进行迭代,从而产生您正在观察的结果。 If you pass a tuple of strings, it gets an actual string, not a single character on every iteration. 如果传递字符串的元组,它将获得实际的字符串,而不是每次迭代都包含单个字符。

To put it another way - if you add square brackets around the whole output, it will be treated as one item, so commas won't be added.换句话说——如果您在整个 output 周围添加方括号,它将被视为一个项目,因此不会添加逗号。 eg instead of:例如,而不是:

spamwriter.writerow(matrix[row]['id'],matrix[row]['value'])

use:使用:

spamwriter.writerow([matrix[row]['id'] + ',' + matrix[row]['value']])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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