首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python中使用漂亮汤时出错

在Python中使用漂亮汤时出错
EN

Stack Overflow用户
提问于 2015-04-20 02:45:50
回答 2查看 4.3K关注 0票数 1

我的代码很好用。但是,对于某些数据,我的代码会出现错误。有问题的数据是:本月t将满10岁。为了纪念这一周年和即将发行的T@10期,本系列回顾了第一个十年杂志中一些最令人难忘的故事。

报告的问题是

追溯(最近一次调用):文件"/Users/mas/Documents/workspace/DeepLearning/BagOfWords.py",第41行,在clean_train_reviews.append(“".join(KaggleWord2VecUtility.review_to_wordlist(train"Snippet",True”))文件"/Users/mas/Documents/workspace/DeepLearning/KaggleWord2VecUtility.py",行22,在review_to_wordlist review_text = BeautifulSoup(review).get_text() File .get_text第162行中,init elif (标记) <= 256: TypeError类型的对象中没有len()

守则:

代码语言:javascript
复制
def deprecated_argument(old_name, new_name):
        if old_name in kwargs:
            warnings.warn(
                'The "%s" argument to the BeautifulSoup constructor '
                'has been renamed to "%s."' % (old_name, new_name))
            value = kwargs[old_name]
            del kwargs[old_name]
            return value
        return None

    parse_only = parse_only or deprecated_argument(
        "parseOnlyThese", "parse_only")

    from_encoding = from_encoding or deprecated_argument(
        "fromEncoding", "from_encoding")

    if len(kwargs) > 0:
        arg = kwargs.keys().pop()
        raise TypeError(
            "__init__() got an unexpected keyword argument '%s'" % arg)

    if builder is None:
        if isinstance(features, basestring):
            features = [features]
        if features is None or len(features) == 0:
            features = self.DEFAULT_BUILDER_FEATURES
        builder_class = builder_registry.lookup(*features)
        if builder_class is None:
            raise FeatureNotFound(
                "Couldn't find a tree builder with the features you "
                "requested: %s. Do you need to install a parser library?"
                % ",".join(features))
        builder = builder_class()
    self.builder = builder
    self.is_xml = builder.is_xml
    self.builder.soup = self

    self.parse_only = parse_only

    if hasattr(markup, 'read'):        # It's a file-type object.
        markup = markup.read()
    elif len(markup) <= 256:
        # Print out warnings for a couple beginner problems
        # involving passing non-markup to Beautiful Soup.
        # Beautiful Soup will still parse the input as markup,
        # just in case that's what the user really wants.
        if (isinstance(markup, unicode)
            and not os.path.supports_unicode_filenames):
            possible_filename = markup.encode("utf8")
        else:
            possible_filename = markup
        is_file = False
        try:
            is_file = os.path.exists(possible_filename)
        except Exception, e:
            # This is almost certainly a problem involving
            # characters not valid in filenames on this
            # system. Just let it go.
            pass
        if is_file:
            warnings.warn(
                '"%s" looks like a filename, not markup. You should probably open this file and pass the filehandle into Beautiful Soup.' % markup)
        if markup[:5] == "http:" or markup[:6] == "https:":
            # TODO: This is ugly but I couldn't get it to work in
            # Python 3 otherwise.
            if ((isinstance(markup, bytes) and not b' ' in markup)
                or (isinstance(markup, unicode) and not u' ' in markup)):
                warnings.warn(
                    '"%s" looks like a URL. Beautiful Soup is not an HTTP client. You should probably use an HTTP client to get the document behind the URL, and feed that document to Beautiful Soup.' % markup)

    for (self.markup, self.original_encoding, self.declared_html_encoding,
     self.contains_replacement_characters) in (
        self.builder.prepare_markup(markup, from_encoding)):
        self.reset()
        try:
            self._feed()
            break
        except ParserRejectedMarkup:
            pass

    # Clear out the markup and remove the builder's circular
    # reference to this object.
    self.markup = None
    self.builder.soup = None

这是我的主要代码:

代码语言:javascript
复制
import os
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.ensemble import RandomForestClassifier
from KaggleWord2VecUtility import KaggleWord2VecUtility
import pandas as pd
import numpy as np

if __name__ == '__main__':
    train = pd.read_csv(os.path.join(os.path.dirname(__file__), 'data', 'NYTimesBlogTrain.csv'), header=0)
    test = pd.read_csv(os.path.join(os.path.dirname(__file__), 'data', 'NYTimesBlogTest.csv'), header=0)

    print 'A sample Abstract is:'
    print train["Abstract"][2838]

    print 'A sample Snippet is:'
    print train["Snippet"][2838]
    #raw_input("Press Enter to continue...")


    #print 'Download text data sets. If you already have NLTK datasets downloaded, just close the Python download window...'
    #nltk.download()  # Download text data sets, including stop words

    # Initialize an empty list to hold the clean reviews
    clean_train_reviews = []

    # Loop over each review; create an index i that goes from 0 to the length
    # of the movie review list
    print len(train["Snippet"])
    print "Cleaning and parsing the training set abstracts...\n"
    for i in xrange( 0, 3000):
        clean_train_reviews.append(" ".join(KaggleWord2VecUtility.review_to_wordlist(train["Snippet"][i], True)))
        if not train["Snippet"][i]:
            print i  
#  
EN

回答 2

Stack Overflow用户

发布于 2017-04-04 17:57:45

对我来说,这个问题是由一些样本在评审功能中根本不包含数据造成的。您可以使用以下方法更改此操作,并将不带任何评论的示例设置为空白:

代码语言:javascript
复制
train = train.fillna(" ")
票数 2
EN

Stack Overflow用户

发布于 2015-04-20 03:18:18

如果没有看到完整的上下文(例如传递给构造函数的review值),那么您的KaggleWord2VecUtility方法是否有可能在@符号和/或数字上分裂,这样一个令牌被作为浮点数传递,而不是字符串/unicode对象?当init需要字符串或unicode对象时,异常表示markup是意外的浮点数。

代码语言:javascript
复制
def __init__(self, markup="", features=None, builder=None,
             parse_only=None, from_encoding=None, **kwargs):
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29738545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档