简体   繁体   English

AttributeError: 'NoneType' object 在写入文件时没有属性 'get'

[英]AttributeError: 'NoneType' object has no attribute 'get' when writing into a file

I am trying to scrape (using a loop for different users) from a website and then saving it in a dictionary and writing it in a file.我正在尝试从网站上抓取(对不同用户使用循环),然后将其保存在字典中并将其写入文件中。

The problem is users are in hundreds and not every user has same data in json.问题是用户数以百计,并不是每个用户在 json 中都有相同的数据。 For the users which don't have a particular set of rows of data it gives me that error.对于没有特定数据行的用户,它给了我这个错误。

for i in range(10):
    u = User(list_of_users[i])

    try:
        data_list = [[u.name, u.full_name, u.date_of_birth
    ,u.current_age, p.job_info.get('UserId').get('JobId'), p.job_info_with_z.get('UserId').get('location')]]

    except AttributeError:
        data_list = [[u.name, u.full_name, u.date_of_birth
    ,u.current_age, p.job_info.get('UserId').get('JobId'), p.job_info_with_z.get('UserId').get('location')]]
    with open('test_players_data.csv', 'a', newline='') as file:
        writer = csv.writer(file, delimiter=',')
        writer.writerows(data_list)

.name calls a method to get the name, like wise for job_info and info_with_z. .name 调用一个方法来获取名称,就像 job_info 和 info_with_z 一样。

so this works for majority of the users but for users who don't have "UserId" it gives me the above error.所以这适用于大多数用户,但对于没有“UserId”的用户,它会给我上述错误。 But there are some users who will have UserId but not UserIdWithZ.但是有些用户会有 UserId 但没有 UserIdWithZ。 I want to skip those with some default value and keep on scraping data and write it in the file.我想跳过具有一些默认值的那些并继续抓取数据并将其写入文件中。 Once it fails, it doesn't go forward.一旦失败,它就不会向前 go 前进。

There has to be a way to give default values where keys are not present.必须有一种方法可以在不存在键的情况下提供默认值。 I have trimmed data.我已经修剪了数据。 So every user has more than 30 different values that I am trying to scrape in 4 blocks of code.所以每个用户都有超过 30 个不同的值,我试图在 4 个代码块中抓取它们。

example data示例数据

{
'UserId':  {'jobId': '74', 'jobLocation': '72', 'jobType': '10'},
'UserIdWithZ: {'jobId': '74', 'jobLocation': '72', 'jobType': '10'}
}

I just want that row/record in.csv to populate '' or '-' or 'na' where there isn't any data and not just fail.我只希望在.csv 中的行/记录填充没有任何数据而不仅仅是失败的“”或“-”或“na”。

Thanks in advance提前致谢

Specify the default value using .get so in your case,在您的情况下使用.get指定默认值,

p.job_info.get('UserId', {}).get('JobId', '-')

If you are expecting a dict to chain the next get you need to specify the default value as {} .如果您希望dict链接下一个get您需要将默认值指定为{} If there aint no value for job_info.get('UserId') , None will be the default value, which doesnt have get method.如果job_info.get('UserId')没有值,则None将是默认值,它没有get方法。

Do the same for all the .get methods,对所有.get方法执行相同操作,

暂无
暂无

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

相关问题 AttributeError: 'NoneType' object 在写入文件时没有属性 'encode'? - AttributeError: 'NoneType' object has no attribute 'encode' while writing into file? AttributeError: 'NoneType' object 没有属性 'get_screenshot_as_file' - AttributeError: 'NoneType' object has no attribute 'get_screenshot_as_file' AttributeError:当我保存在age中时,“ NoneType”对象没有属性“ get” - AttributeError: 'NoneType' object has no attribute 'get' when I save in amage Flasgger AttributeError:'NoneType'对象没有属性'get'吗? - Flasgger AttributeError: 'NoneType' object has no attribute 'get'? AttributeError: 'NoneType' 对象没有属性 'name' 错误在我尝试获取 docx 文件的标题时发生 - AttributeError: 'NoneType' object has no attribute 'name' error occurs when I try to get a heading of a docx file AttributeError: 'NoneType' object 没有属性 'get' helpp - AttributeError: 'NoneType' object has no attribute 'get' helpp 'NoneType' object 没有属性 'get':AttributeError - 'NoneType' object has no attribute 'get': AttributeError AttributeError: 'NoneType' 对象没有属性 'data' - 当我正在编写一个函数来将一个元素插入到排序的链表中时 - AttributeError: 'NoneType' object has no attribute 'data' - when I'm writing a function to insert an element into sorted linked list AttributeError: 'NoneType' 对象没有属性 - AttributeError: 'NoneType' object has no attribute AttributeError:“ NoneType”对象没有属性“ a” - AttributeError: 'NoneType' object has no attribute 'a'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM