【问题标题】:What am I doing wrong in this split for excel rows loop?在这个拆分为 excel 行循环中我做错了什么?
【发布时间】:2019-06-17 06:34:37
【问题描述】:

我编写了这个小代码来从 excel 工作簿中的 url 列表中拆分域。但问题是我无法将其写入实际工作簿,甚至无法使用域创建新工作簿。

#looping it
for i in range(2, 200):
print((sheet.cell(row=i, column=1).value).split('http://')[-1].split('/')[0].split('www.')[-1])

#but by this i have to go copy paste the results in the excel sheet
#so i tried this replace value method but it keeps showing attribute error

for x in range(2, 258):
    sheet.cell(row=x, column=1).value = sheet.cell(row=x, column=1).value.split('http://')[-1].split('/')[0].split('www.')[-1]


Traceback (most recent call last):
  File "<pyshell#63>", line 2, in <module>
    sheet.cell(row=x, column=1).value = sheet.cell(row=x, column=1).value.split('http://')[-1].split('/')[0].split('www.')[-1]
AttributeError: 'NoneType' object has no attribute 'split'

我希望这个循环遍历列表并将 url(htts://www.example.com/example-page/) 拆分为域(example.com) 并将其保存在同一张表或新表中一世 用这个

wb.save('domains_list')
#it saves the splitted domains automatically without me copy pasting it from idle to excel workbook.

【问题讨论】:

  • 您的某些单元格不包含任何字符串。

标签: python excel openpyxl attributeerror nonetype


【解决方案1】:

就像在 cmets 中所说,您必须管理单元格为空的情况:

for i in range(2, 200):
    val = sheet.cell(row=i, column=1).value
    if val is None:
        continue   
    print(val.split('http://')[-1].split('/')[0].split('www.')[-1])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 2018-08-12
    • 1970-01-01
    • 2022-12-17
    相关资源
    最近更新 更多