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

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

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

问题

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

I have the following code:

  1. for car in car_list:
  2. specs = car.find_elements(By.XPATH, ".//li[@class='sc-kcuKUB bMKvpn']")
  3. car_specs = []
  4. for spec in specs:
  5. entry = spec.get_attribute('innerHTML')
  6. entry = spec.text
  7. car_specs.append(entry)
  8. 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:

  1. for spec in specs:
  2. if spec is None:
  3. car_specs.append('texthere')
  4. else:
  5. entry = spec.get_attribute('innerHTML')
  6. entry = spec.text
  7. car_specs.append(entry)

答案1

得分: 1

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

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

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

  1. all_specs = []
  2. for car in car_list:
  3. specs = car.find_elements(By.XPATH, ".//li[@class='sc-kcuKUB bMKvpn']")
  4. car_specs = [
  5. spec.text.strip()
  6. for spec in specs
  7. if spec.text.strip()
  8. ]
  9. 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)

英文:
  1. for car in car_list:
  2. specs = car.find_elements(By.XPATH, ".//li[@class='sc-kcuKUB bMKvpn']")
  3. car_specs = []
  4. if len(specs) == 0:
  5. car_specs.append('texthere')
  6. for spec in specs:
  7. entry = spec.get_attribute('innerHTML')
  8. entry = spec.text
  9. car_specs.append(entry)
  10. 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:

确定