本文整理匯總了Python中bs4.UnicodeDammit.strip方法的典型用法代碼示例。如果您正苦於以下問題:Python UnicodeDammit.strip方法的具體用法?Python UnicodeDammit.strip怎麽用?Python UnicodeDammit.strip使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類bs4.UnicodeDammit
的用法示例。
在下文中一共展示了UnicodeDammit.strip方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: create
# 需要導入模塊: from bs4 import UnicodeDammit [as 別名]
# 或者: from bs4.UnicodeDammit import strip [as 別名]
def create(self, soupfragment):
result = dict()
field = self._getfield_info(soupfragment)
title = ""
result["link"] = ""
result["answers"] = ""
result["views"] = ""
result["location"] = ""
if self.urlobject is not None:
result["location"] = self.urlobject.description()
#result['location'] = self.webclient.get_url_desc()
if field is not None:
title = UnicodeDammit(field.a.contents[0]).unicode_markup
result["link"] = field.a['href']
fragment = self._get_answer_and_viewa_fragment(soupfragment)
if fragment is not None:
result["answers"] = self._get_number_from(fragment.contents[0].strip())
result["views"] = self._get_number_from(fragment.contents[2].strip())
else:
print "No answer and view bloq identified in thread: ", result["link"]
result["answers"] = -1
result["views"] = -1
result["title"] = title.strip()
#result['next_url'] = _nextUrl(soupfragment)
return result
示例2: _sub_read
# 需要導入模塊: from bs4 import UnicodeDammit [as 別名]
# 或者: from bs4.UnicodeDammit import strip [as 別名]
def _sub_read(self, f):
example_num = 0
curr_id = 'EXAMPLE_0'
for line in f:
# Process encoding
if not isinstance(line, text_type):
line = UnicodeDammit(line, ['utf-8',
'windows-1252']).unicode_markup
line = line.strip()
# Handle instance lines
if line.startswith('#'):
curr_id = line[1:].strip()
elif line and line not in ['TRAIN', 'TEST', 'DEV']:
split_line = line.split()
num_cols = len(split_line)
del line
# Line is just a class label
if num_cols == 1:
class_name = safe_float(split_line[0],
replace_dict=self.class_map)
field_pairs = []
# Line has a class label and feature-value pairs
elif num_cols % 2 == 1:
class_name = safe_float(split_line[0],
replace_dict=self.class_map)
field_pairs = split_line[1:]
# Line just has feature-value pairs
elif num_cols % 2 == 0:
class_name = None
field_pairs = split_line
curr_info_dict = {}
if len(field_pairs) > 0:
# Get current instances feature-value pairs
field_names = islice(field_pairs, 0, None, 2)
# Convert values to floats, because otherwise
# features'll be categorical
field_values = (safe_float(val) for val in
islice(field_pairs, 1, None, 2))
# Add the feature-value pairs to dictionary
curr_info_dict.update(zip(field_names, field_values))
if len(curr_info_dict) != len(field_pairs) / 2:
raise ValueError(('There are duplicate feature ' +
'names in {} for example ' +
'{}.').format(self.path_or_list,
curr_id))
yield curr_id, class_name, curr_info_dict
# Set default example ID for next instance, in case we see a
# line without an ID.
example_num += 1
curr_id = 'EXAMPLE_{}'.format(example_num)
示例3: corpus_generator
# 需要導入模塊: from bs4 import UnicodeDammit [as 別名]
# 或者: from bs4.UnicodeDammit import strip [as 別名]
def corpus_generator(self):
with open(self.corpus_path, 'rb') as f:
i = 0
for line in f:
line = UnicodeDammit(line.strip()).unicode_markup
if line:
if self.lower:
line = line.lower()
i += 1
if i % 100000 == 0:
logging.info('Read {} nonblank lines'.format(i))
for tok in re.split(r'\s+', line):
yield tok
示例4: document_generator
# 需要導入模塊: from bs4 import UnicodeDammit [as 別名]
# 或者: from bs4.UnicodeDammit import strip [as 別名]
def document_generator(path, lower=False):
'''
Default document reader. Takes a path to a file with one document per line,
with tokens separate by whitespace, and yields lists of tokens per document.
This could be replaced by any function that yields lists of tokens.
See main() for how it is called.
Note: this uses BeautifulSoup's UnicodeDammit to convert to unicode.
'''
with open(path, 'rb') as f:
i = 0
for line in f:
line = UnicodeDammit(line.strip()).unicode_markup
if line:
if lower:
line = line.lower()
i += 1
if i % 100000 == 0:
logging.info('Read {} nonblank lines'.format(i))
yield re.split(r'\s+', line)
示例5: clean_google_title
# 需要導入模塊: from bs4 import UnicodeDammit [as 別名]
# 或者: from bs4.UnicodeDammit import strip [as 別名]
def clean_google_title(self, title):
has_dot = False
titleCleaned = UnicodeDammit(title).unicode_markup
# clean step 1
# BUGFIX: don't remove [xxx]. eg: "OQL[C++]: Ext...'
titleCleaned = re.sub("(<(.*?)>)", "", titleCleaned)
re_hasdot = re.compile("(\.\.\.|…)", re.I)
match = re_hasdot.search(title)
if match is not None:
has_dot = True
# clean step 2, here title is readable
titleCleaned = re.sub("( |►|…)", "", titleCleaned)
titleCleaned = re.sub("(&#.+?;|&.+?;)", "", titleCleaned)
titleCleaned = titleCleaned.strip()
readableTitle = titleCleaned
# Shrink, only letters left
titleCleaned = re.sub("\W", "", titleCleaned)
titleCleaned = titleCleaned.lower()
return (readableTitle, titleCleaned, has_dot)
示例6: print
# 需要導入模塊: from bs4 import UnicodeDammit [as 別名]
# 或者: from bs4.UnicodeDammit import strip [as 別名]
print()
print()
print("Start by telling me when the introduction will end.")
for block in script_text.descendants:
# Si block est une instance de bs4.Tag, il est entouré de balises HTML
# Le prochain block contiendra le même texte sans les balises
# Donc on continue sans parser ce bloc
if(isinstance(block, Tag)):
continue
# UnicodeDammit converts any string to UTF-8
# does not work so well
block = UnicodeDammit(block, soup.original_encoding).unicode_markup
# remove leading and ending end of lines
block = block.strip('\n')
# if the block doesn't have any text, skip it
if( re.search('\w', block) == None ):
continue
# bs4 ne coupe pas toujours bien les différents blocs
# Mieux vaut donc redécouper par paragraphe et les traiter un à un
for line in block.split('\n'):
stripped_line = line.strip(' \n\t\r')
if( re.search('\w', line) == None ):
continue
print('------------------------------ Begin line ------------------------------')
print(line)
print(' ------- End line -------')