繁体   English   中英

如何从 python 的列表中删除某些元素?

[英]How do I remove certain elements from a list in python?

我正在从网站上抓取一些数据,但其中一些数据前面带有“\”。 我尝试使用此代码字符串,但出现错误消息。

print([s.strip('\') for s in feet])    *EOL while scanning string literal
print([s.replace('\', ') for s in feet])

第一行中'\'之后的代码变成了斜体,我不知道该怎么做。

from lxml import html
import requests

list1 = []
height = []

user_website = "https://www.disabled-world.com/calculators-charts/height-weight.php"

page = requests.get(user_website)
tree = html.fromstring(page.content)
list2 = tree.xpath('//td/text()')

for x in list2:
    list_holder = x.split(" ")
    for i in list_holder:
        list1.append(i.lower())

subs = "'"
feet = [i for i in list2 if subs in i]

subs2 = '"'
inches = [i for i in list2 if subs in i]

print([s.strip('\') for s in feet])
print([s.replace('\', ') for s in feet])

y = 0

for x in feet:
    height.append(feet[y])
    height.append(inches[y])
    y+=1

print(height)

所以我试图提取你的代码,这是:

from lxml import html 
import requests 

list1 = [] 
height = [] 
user_website = "https://disabled-world.com/calculators-charts/height-weight.php" 
page = requests.get(user_website) 
tree = html.fromstring(page.content) 
list2 = tree.xpath('//td/text()')

for x in list2: 
    list_holder = x.split(" ") 
    for i in list_holder: 
        list1.append(i.lower()) 
subs = "'" 
feet = [i for i in list2 if subs in i] 
subs2 = '"' 
inches = [i for i in list2 if subs in i] 
print([s.strip('"') for s in feet]) 
#print([s.replace('\', ') for s in feet]) 
y = 0 

#for x in feet: 
#    height.append(feet[y]) 
#    height.append(inches[y]) 
#    y+=1 
#    print(height)

给我以下 output:

["4' 6", "4' 7", "4' 8", "4' 9", "4' 10", "4' 11", "5' 0", "5' 1", "5' 2", "5' 3", "5' 4", "5' 5", "5' 6", "5' 7", "5' 8", "5' 9", "5' 10", "5' 11", "6' 0", "6' 1", "6' 2", "6' 3", "6' 4", "6' 5", "6' 6", "6' 7", "6' 8", "6' 9", "6' 10", "6' 11", "7' 0"]

根据您的问题,我认为这就是您想要的?

无论如何,问题(据我所见)只是错误使用strip() function,它需要一个字符串(包含您要剥离的源字符串的一部分),而不仅仅是一个字符。

在您发布代码之前,很难弄清楚为什么您的数组格式不正确的错误。 但是,您可以使用以下方法来修复此数组:

#copy paste the data into a variable as a string using triple quotes (or convert this variable to a string)
a='''['4' 6"', '4' 6"', '4' 7"', '4' 7"', '4' 8"', '4' 8"', '4' 9"', '4' 9"', '4' 10"', '4' 10"', '4' 11"', '4' 11"', '5' 0"', '5' 0"', '5' 1"', '5' 1"', '5' 2"', '5' 2"', '5' 3"', '5' 3"', '5' 4"', '5' 4"', '5' 5"', '5' 5"', '5' 6"', '5' 6"', '5' 7"', '5' 7"', '5' 8"', '5' 8"', '5' 9"', '5' 9"', '5' 10"', '5' 10"', '5' 11"', '5' 11"', '6' 0"', '6' 0"', '6' 1"', '6' 1"', '6' 2"', '6' 2"', '6' 3"', '6' 3"', '6' 4"', '6' 4"', '6' 5"', '6' 5"', '6' 6"', '6' 6"', '6' 7"', '6' 7"', '6' 8"', '6' 8"', '6' 9"', '6' 9"', '6' 10"', '6' 10"', '6' 11"', '6' 11"', '7' 0"', '7' 0"']'''

m=a.strip('[').strip(']')  #remove braces
x=[]                       
n=m.split(',')             #creaet list of elements
for i in n:
    x.append(i.strip(" ").strip("'"))  #remove the excessive quotes and spaces
print(x)

上面的代码给了我 x 为:

['4\' 6"', '4\' 6"', '4\' 7"', '4\' 7"', '4\' 8"', '4\' 8"', '4\' 9"', '4\' 9"', '4\' 10"', '4\' 10"', '4\' 11"', '4\' 11"', '5\' 0"', '5\' 0"', '5\' 1"', '5\' 1"', '5\' 2"', '5\' 2"', '5\' 3"', '5\' 3"', '5\' 4"', '5\' 4"', '5\' 5"', '5\' 5"', '5\' 6"', '5\' 6"', '5\' 7"', '5\' 7"', '5\' 8"', '5\' 8"', '5\' 9"', '5\' 9"', '5\' 10"', '5\' 10"', '5\' 11"', '5\' 11"', '6\' 0"', '6\' 0"', '6\' 1"', '6\' 1"', '6\' 2"', '6\' 2"', '6\' 3"', '6\' 3"', '6\' 4"', '6\' 4"', '6\' 5"', '6\' 5"', '6\' 6"', '6\' 6"', '6\' 7"', '6\' 7"', '6\' 8"', '6\' 8"', '6\' 9"', '6\' 9"', '6\' 10"', '6\' 10"', '6\' 11"', '6\' 11"', '7\' 0"', '7\' 0"']

暂无
暂无

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

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