简体   繁体   English

我在excel行循环的拆分中做错了什么?

[英]What am I doing wrong in this split for excel rows loop?

I wrote this little code to split domains from lists of urls in excel workbook. 我写了这个小代码,用于从excel工作簿中的url列表中分割域名。 But the problem is i can't get it to write in the actual workbookt or even create a new workbook with the domains. 但问题是我无法在实际的工作簿中编写,甚至无法使用域创建新的工作簿。

#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'

I want this loop go through the list and split the url(htts://www.example.com/example-page/) to domain(example.com) and save it in the same sheet or a new sheet when i use this 我希望这个循环遍历列表并将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.

Like said in the comments, you have to manage the case where a cell is empty: 如评论中所述,您必须管理单元格为空的情况:

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])

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

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