Need Help with the BeautifulSoup problem, please
I need to replace all tag with after ■. But the result from below is '■ D / ' Can you explain what I did wrong, please. s = '■A B C D / ' soup = BeautifulSoup(s) for i in soup.find_all(text='■'): tag = soup.new_tag('span') tag['style'] = 'REPLACE' for ii in i.find_next_siblings(): if ii.name=='font' or str(ii).lstrip('')[0:1]=='/': break else: if ii.name=='b': tag.string=ii.string print(ii.replace_with(tag)) print(soup) -- https://mail.python.org/mailman/listinfo/python-list
Re: Need Help with the BeautifulSoup problem, please
seas...@gmail.com於 2013年12月16日星期一UTC+8下午2時41分08秒寫道: > I need to replace all tag with after ■. But the result from below > is '■ D / ' > > Can you explain what I did wrong, please. > > > > s = '■A B C D / ' > > soup = BeautifulSoup(s) > > for i in soup.find_all(text='■'): > > tag = soup.new_tag('span') > > tag['style'] = 'REPLACE' > > for ii in i.find_next_siblings(): > > if ii.name=='font' or str(ii).lstrip('')[0:1]=='/': > > break > > else: > > if ii.name=='b': > > tag.string=ii.string > > print(ii.replace_with(tag)) > > print(soup) the point is the result seems wrong. I don't know if that is my problem. I simplify the code to emphasize the problem, there's no way an editor can do what I wanna do. -- https://mail.python.org/mailman/listinfo/python-list
Re: Need Help with the BeautifulSoup problem, please
8 Dihedral於 2013年12月16日星期一UTC+8下午4時02分42秒寫道: > On Monday, December 16, 2013 2:41:08 PM UTC+8, seas...@gmail.com wrote: > > > I need to replace all tag with after ■. But the result from > > below is '■ D / ' > > > > > > Can you explain what I did wrong, please. > > > > > > > > > > > > s = '■A B C D / ' > > > > > > soup = BeautifulSoup(s) > > > > > > for i in soup.find_all(text='■'): > > > > > > tag = soup.new_tag('span') > > > > > > tag['style'] = 'REPLACE' > > > > > > for ii in i.find_next_siblings(): > > > > > > if ii.name=='font' or str(ii).lstrip('')[0:1]=='/': > > > > > > break > > > > > > else: > > > > > > if ii.name=='b': > > > > > > tag.string=ii.string > > > > > > print(ii.replace_with(tag)) > > > > > > print(soup) > > > > I think you should try some descent > > free editors such as notepad++ > > for your source codes to > > replace trivial strings without > > programmig. I think it's my fault, thanks -- https://mail.python.org/mailman/listinfo/python-list
should I transfer 'iterators' between functions?
take the following as an example, which could work well. But my concern is, will list 'l' be deconstructed after function return? and then iterator point to nowhere? def test(): l = [1, 2, 3, 4, 5, 6, 7, 8] return iter(l) def main(): for i in test(): print(i) -- https://mail.python.org/mailman/listinfo/python-list
how to iterate all sub-element in a list
a list like L = [[1, 2], [3, 4, 5], [6]], which has random numbers of list, which has random numbers. How do I get another list m = [1, 2, 3, 4, 5, 6] in a succinct way? ( or iterate them) I should provide my own solution, but I really can't come out with one. -- https://mail.python.org/mailman/listinfo/python-list
Re: how to iterate all sub-element in a list
Skip Montanaro於 2014年1月31日星期五UTC+8下午6時29分27秒寫道: > Google for "python flatten list." > > > > This question comes up frequently, though I'm not sure if that's > > because it's a common homework problem or because people are unaware > > of the += operator (or extend method) for lists, and so build > > lists-of-lists when they could just build them flat in the first > > place. > > > > Skip thanks. a keyword is all I need. It is not homework. It is a common problem I guess. -- https://mail.python.org/mailman/listinfo/python-list
Re: how to iterate all sub-element in a list
Mark Lawrence於 2014年1月31日星期五UTC+8下午10時48分46秒寫道: > I'm pleased to see that you have answers. In return would you please read and action this https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing the double line spacing above, thanks. > My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. > Mark Lawrence My bad, I replied in a hurry with my mobile. -- https://mail.python.org/mailman/listinfo/python-list