Python:在使用for循环创建列表时,如果为空插入值?

huangapple go评论61阅读模式
英文:

Python: When using a for loop to create list, if blank insert value?

问题

for car in car_list:
    specs = car.find_elements(By.XPATH, ".//li[@class='sc-kcuKUB bMKvpn']")
    car_specs = []
    for spec in specs:
        entry = spec.get_attribute('innerHTML')
        entry = spec.text
        if not entry.strip():  # Check if entry is blank or consists of only whitespace
            car_specs.append('texthere')  # Insert 'texthere' if spec is blank
        else:
            car_specs.append(entry)
    all_specs.append(car_specs)
英文:

I have the following code:

 for car in car_list:
        specs = car.find_elements(By.XPATH, ".//li[@class='sc-kcuKUB bMKvpn']")
        car_specs = []
        for spec in specs:
                entry = spec.get_attribute('innerHTML')
                entry = spec.text
                car_specs.append(entry)
        all_specs.append(car_specs)

Which correctly produces output such as the following:

print(all_specs):

> ['2021 (21 reg)', 'SUV', '35,008 miles', '395BHP', 'Automatic',
> 'Electric', '1 owner'] [['2021 (21 reg)', 'SUV', '35,008 miles',
> '395BHP', 'Automatic', 'Electric', '1 owner']]

However, sometimes the specs for a car are blank, resulting in the following:

> ['2021 (21 reg)', 'SUV', '35,008 miles', '395BHP', 'Automatic',
> 'Electric', '1 owner'] [['2021 (21 reg)', 'SUV', '35,008 miles',
> '395BHP', 'Automatic', 'Electric', '1 owner'] []]

(Note the empty list at the end)

When I come to concat this dataframe to another, there is a row transposition issue as there is a blank list in this dataframe, but not in the other.

How can I insert a text value to at least populate the list if spec or specs are blank?

I have tried the following:

for spec in specs:
            if spec is None:
                car_specs.append('texthere')
            else:
                entry = spec.get_attribute('innerHTML')
                entry = spec.text
                car_specs.append(entry)

答案1

得分: 1

如果汽车没有规格,那么你可以直接跳过添加它或者添加一个占位符:

all_specs = []
for car in car_list:
    specs = car.find_elements(By.XPATH, ".//li[@class='sc-kcuKUB bMKvpn']")
    car_specs = [
        spec.text.strip()
        for spec in specs
        if spec.text.strip()
    ]
    all_specs.append(car_specs or ["未找到规格"])
英文:

If the car has no specs, then you can just bypass adding it or add a placeholder:

all_specs = []
for car in car_list:
    specs = car.find_elements(By.XPATH, ".//li[@class='sc-kcuKUB bMKvpn']")
    car_specs = [
        spec.text.strip()
        for spec in specs
        if spec.text.strip()
    ]
    all_specs.append(car_specs or ["no specs found"])

答案2

得分: 0

for car in car_list:
specs = car.find_elements(By.XPATH, ".//li[@class='sc-kcuKUB bMKvpn']")
car_specs = []
if len(specs) == 0:
car_specs.append('texthere')
for spec in specs:
entry = spec.get_attribute('innerHTML')
entry = spec.text
car_specs.append(entry)
all_specs.append(car_specs)

英文:
for car in car_list:
    specs = car.find_elements(By.XPATH, ".//li[@class='sc-kcuKUB bMKvpn']")
    car_specs = []
    if len(specs) == 0:
        car_specs.append('texthere')
    for spec in specs: 
            entry = spec.get_attribute('innerHTML')
            entry = spec.text
            car_specs.append(entry)
    all_specs.append(car_specs)

Edit: I edited my previous answer because I did read your problem wrong

Your problem is probably caused by the fact that if specs is empty ( 0 size list), you append an empty list ( car_specs = [] ) to your definitive list

To solve this problem, at each iteration check the size of specs, if it's 0 then you know it's empty

huangapple
  • 本文由 发表于 2023年6月5日 21:33:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定