NoneType has no attribute IF-ELSE solution

2024/10/13 9:21:13

I'm parsing an HTML file and searching for status of order in it. Sometimes, status doesn't exist, so BeautifulSoup returns NoneType, when I'm using it. To solve this problem I use if-else statement, but it doesn't work too. Python returns:

TypeError: object of type 'NoneType' has no len()

I am adding status to a dictionary, which contains other info from the order. The code below is a part, which adds new order's info to a dict.

database.append({"Title": title[0] + title.lower()[1:],"Name": name[0].upper() + name[1:],"Status": status.string[:len(status.string)-3] if status is not None else "Not listed","Price": price})
Answer

There seem to be three cases:

  1. status is None
  2. status is not None, but status.string is
  3. both status and status.string are not None

You have to provide code that handles each case.

if status is not None and status.string is not None:st = status.string[:-3]
else:st = "Not listed"database.append({"Title": title[0] + title.lower()[1:],"Name": name[0].upper() + name[1:],"Status": st,"Price": price})

You can also stick the whole if in the dict if you prefer. You could also use exceptions:

try:st = status.string[:-3]
except (TypeError, AttributeError):st = "Not listed"
https://en.xdnf.cn/q/118098.html

Related Q&A

looking for an inverted heap in python

Id like to comb off the n largest extremes from a timeseries. heapq works perfectly for the nlargestdef nlargest(series, n):count = 0heap = []for e in series:if count < n:count+=1hp.heappush(heap, e…

Concatenating Multiple DataFrames with Non-Standard Columns

Is there a good way to concatenate a list of DataFrames where the columns are not regular between DataFrames? The desired outcome is to match up all columns that are a match but to keep the ones that …

Python Conditionally Add Class to td Tags in HTML Table

I have some data in the form of a csv file that Im reading into Python and converting to an HTML table using Pandas.Heres some example data:name threshold col1 col2 col3 A 10 12 9 13…

Sort a dictionary of dictionaries python

I have a dictionary of dictionaries like the followingd = {hain: {facet: 1, wrapp: 1, chinoiserie: 1}, library: {sconc: 1, floor: 1, wall: 2, lamp: 6, desk: 1, table: 1, maine: 1} }So, I want to rever…

How can I get python generated excel document to correctly calculate array formulas

I am generating some excel files with python using python 3.6 and openpyxl.At one point I have to calculate standard deviations of a subsection of data. In excel this is done with an array formula. Wri…

Unable to locate element in Python Selenium

Im trying to locate an element using python selenium, and have the following code:zframe = driver.find_element_by_xpath("/html/frameset/frameset/frame[5]") driver.switch_to.frame(zframe) find…

How to import a variable from a different class

I have an instance of a class that i set the value to self.world inside a class named zeus inside a module named Greek_gods. and i have another class names World inside a module name World.How can i te…

Scrapy: AttributeError: YourCrawler object has no attribute parse_following_urls

I am writing a scrapy spider. I have been reading this question: Scrapy: scraping a list of links, and I can make it recognise the urls in a listpage, but I cant make it go inside the urls and save the…

initializer is not a constant, error C2099, on compiling a module written in c for python

i tried to compile a python module called distance, whith c "python setup.py install --with-c" using msvc 2017 on windows 10, i got this error ,Cdistance / distance.c (647): error C2099: init…

How can make pandas columns compare check cell?

I have a two file. a.txt has the below data.Zone,Aliase1,Aliase2 VNX7600SPB3_8B3_H1,VNX7600SPB3,8B3_H1 VNX7600SPBA_8B4_H1,VNX7600SPA3,8B4_H1 CX480SPA1_11B3_H1,CX480SPA1,11B3_H1 CX480SPB1_11B4_H1,CX480S…