1

我试图从抓取的数据中删减几个词。

3 Bedroom, Residential Apartment in Velachery

有很多行这样的数据。我正在尝试从字符串中删除“卧室”一词。我正在使用漂亮的汤和 python 来抓取网页,这里我正在使用这个

for eachproperty in properties:
 print eachproperty.string[2:]

我知道上面的代码会做什么。但我不知道如何删除介于 3 和 ,Residen 之间的“卧室”....

4

3 回答 3

1
>>> import re   
>>> strs = "3 Bedroom, Residential Apartment in Velachery"
>>> re.sub(r'\s*Bedroom\s*', '', strs)
'3, Residential Apartment in Velachery'

或者:

>>> strs.replace(' Bedroom', '')
'3, Residential Apartment in Velachery'

请注意,字符串是不可变的,因此您需要将结果分配给re.sub变量str.replace

于 2013-09-16T11:36:45.140 回答
0

你需要的是replace方法:

line = "3 Bedroom, Residential Apartment in Velachery"
line = line.replace("Bedroom", "")

# For multiple lines use a for loop
for line in lines:
    line = line.replace("Bedroom", "")
于 2013-09-16T11:34:53.717 回答
0

一个快速的答案是

k = input_string.split()
if "Bedroom" in k:
  k.remove("Bedroom")
answer = ' '.join(k)

这不会像您的问题那样处理标点符号。为此,您需要

rem = "Bedroom"
answer = ""
for i in range(len(input_string)-len(rem)):
   if (input_string[i:i+len(rem)]==rem):
     answer = input_string[:i]+input_string[i+len(rem)]
     break
于 2013-09-16T11:36:26.680 回答