繁体   English   中英

python我在nonetype中有错误如何解决

[英]python I have error in nonetype how to solve it

在这里,我试图获取公司的域名,公司名称存储在 new.csv 中

我用过的代码

import pandas as pd
import clearbit
import json
clearbit.key = 'sk_1915de5d2d7b6e245d6613e3d2188368'
df = pd.read_csv("/home/vipul/Desktop/new.csv", sep=',', encoding="utf-8")
saved_column = df['Company'].dropna()
print(saved_column)
i=0
res = []
for data in saved_column:   
    n = saved_column[i]
    i = i+1
    data = clearbit.NameToDomain.find(name=n)
    if data is None null() res.append(data['domain'])
    print(res)
df['domain'] = res
df.to_csv("/home/vipul/Desktop/new.csv",index = False, skipinitialspace=False)

print("File saved to desktop as new.csv")

代码的输出

python ts.py
0                ‎Accenture
1              ‎AND Digital
2                ‎Accenture
3    ‎Kite Consulting Group
4                ‎Capgemini
5             ‎Accenture UK
Name: Company, dtype: object
['accenture.com']
['accenture.com', 'and.digital']
['accenture.com', 'and.digital', 'accenture.com']
Traceback (most recent call last):
  File "ts.py", line 15, in <module>
    res.append(data['domain'])
TypeError: 'NoneType' object is not subscriptable

如何在 NoneType 遇到的地方给出一些默认值并将其与 new.csv 中的相应公司名称一起存储

预期输出将保存在 new.csv 中

Company                  domain
‎Accenture                accenture.com
‎AND Digital              and.digital
‎Accenture                accenture.com
‎Kite Consulting Group    None
‎Capgemini                capgemini.com
‎Accenture UK             None

试试这个:

data = clearbit.NameToDomain.find(name=n)
try:
   res.append(data['domain'])
except Exception as e:
   #print(e)
   res.append(None)
print(res)

这对我有用

    data=df.loc[i,'data column']
    try:
        res=clearbit.NameToDomain.find(name=data)['domain']
        df.loc[i,'res']=res
    except TypeError:
        pass

暂无
暂无

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

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