简体   繁体   English

TypeError:__init __()为参数'fieldnames'获得了多个值

[英]TypeError: __init__() got multiple values for argument 'fieldnames'

I have done a web scrape on a website for practice and I am trying to put the data into a pandas dataframe that can be exported to a csv and when I come to a point a TypeError: __init__() got multiple values for argument 'fieldnames' shows up. 我已经在网站上进行了网上抓取练习,并且尝试将数据放入可以导出到csv的pandas数据框中,当我遇到一个TypeError: __init__() got multiple values for argument 'fieldnames'出现了。 I want to write the file using the csv module. 我想使用csv模块写入文件。 Can someone explain how the error has come to happen and how to solve it? 有人可以解释错误如何发生以及如何解决吗? My code is as follows: 我的代码如下:

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import csv

my_url = 'https://www.allagents.co.uk/find-agent/london/'

uClient = uReq(my_url)

page_html = uClient.read()

uClient.close()

page_soup = soup(page_html, 'html.parser')

containers = page_soup.findAll('div', {'class':'itemlabel3'})

filename = "webscrape.csv" 
records = []

for container in containers:
    comp_name   = container.find('div', {'class':'labelleft2 col-md-10'}).div.h4.a.text

    address=container.find('div', {'class':'labelleft2 col-md-10'}).div.p.text.replace('\n','')

    tel         = container.find('div', {'class':'labelleft2 col-md-10'}).div.find('p', {'style':'clear: both; margin-bottom: 15px;'}).strong.text

    records.append({'company': comp_name, 'address': address, 'telephone': tel})
writer = csv.DictWriter(filename, "w", fieldnames=['company', 'address', 'telephone'])

writer.writeheader()

for r in records:
    writer.writerow(r)

Error comes on line : 错误出现在网上:

writer = csv.DictWriter(filename, "w", fieldnames=['company', 'address', 'telephone'])

Thanks in advance, any help is appreciated. 在此先感谢您的帮助。

In csv.DictWriter , the first two arguments are a file object (not a file name) and then fieldnames (which you specified as "w" ). csv.DictWriter ,前两个参数是文件对象(不是文件名),然后是fieldnames (您指定为"w" )。 Try this: 尝试这个:

with open(filename, 'w') as f:
    writer = csv.DictWriter(f, ['company', 'address', 'telephone'])
    # write stuff in this block

I suspect that your call to DictWriter has too many parameters: 我怀疑您对DictWriter的调用有太多参数:

writer = csv.DictWriter(filename, "w", fieldnames ...
                                   ^
                           what is this?

I assume it should be: 我认为应该是:

f = open(filename, 'w')
writer = csv.DictWriter(f, fieldnames ...

In your call the fieldnames parameter is assigned "w" , but you are also supplying your own version as a list. 在您的呼叫中, fieldnames参数被分配为"w" ,但是您还将提供自己的版本作为列表。

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

相关问题 Python TypeError:__ init __()为参数'master'获取了多个值 - Python TypeError: __init__() got multiple values for argument 'master' TypeError:__init __()为关键字参数“ choices”获得了多个值 - TypeError: __init__() got multiple values for keyword argument 'choices' TypeError:“ __ init __()为关键字参数'name'获得了多个值” - TypeError: “__init__() got multiple values for keyword argument 'name'” TypeError: __init__() 为参数 'center' 获得了多个值 - TypeError: __init__() got multiple values for argument 'center' TypeError:__init __()为参数'n_splits'获取了多个值 - TypeError: __init__() got multiple values for argument 'n_splits' 类型错误:__init__() 为参数“strides”获得了多个值 - TypeError: __init__() got multiple values for argument 'strides' TypeError:__init__() 为参数“轴”获取了多个值 - TypeError: __init__() got multiple values for argument 'axes' TypeError:__ init __()得到关键字参数'customer'的多个值 - TypeError: __init__() got multiple values for keyword argument 'customer' 类型错误:__init__() 为参数“index”获得了多个值 - TypeError: __init__() got multiple values for argument 'index' SQLAlchemy TypeError:__init __()为参数'name'获得了多个值 - SQLAlchemy TypeError: __init__() got multiple values for argument 'name'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM