Python float' 对象没有属性 'lower'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27009496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
float' object has no attribute 'lower'
提问by
I'm facing this error and I'm really not able to find the reason for it.
Can somebody please point out the reason for it ?
我正面临这个错误,我真的找不到原因。
有人可以指出原因吗?
for i in tweet_raw.comments:
mns_proc.append(processComUni(i))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-416-439073b420d1> in <module>()
1 for i in tweet_raw.comments:
----> 2 tweet_processed.append(processtwt(i))
3
<ipython-input-414-4e1b8a8fb285> in processtwt(tweet)
4 #Convert to lower case
5 #tweet = re.sub('RT[\s]+','',tweet)
----> 6 tweet = tweet.lower()
7 #Convert www.* or https?://* to URL
8 #tweet = re.sub('((www\.[\s]+)|(https?://[^\s]+))','',tweet)
AttributeError: 'float' object has no attribute 'lower'
A second similar error that facing is this :
面临的第二个类似错误是:
for i in tweet_raw.comments:
tweet_proc.append(processtwt(i))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-423-439073b420d1> in <module>()
1 for i in tweet_raw.comments:
----> 2 tweet_proc.append(processtwt(i))
3
<ipython-input-421-38fab2ef704e> in processComUni(tweet)
11 tweet=re.sub(('[http]+s?://[^\s<>"]+|www\.[^\s<>"]+'),'', tweet)
12 #Convert @username to AT_USER
---> 13 tweet = re.sub('@[^\s]+',' ',tweet)
14 #Remove additional white spaces
15 tweet = re.sub('[\s]+', ' ', tweet)
C:\Users\m1027201\AppData\Local\Continuum\Anaconda\lib\re.pyc in sub(pattern, repl, string, count, flags)
149 a callable, it's passed the match object and must return
150 a replacement string to be used."""
--> 151 return _compile(pattern, flags).sub(repl, string, count)
152
153 def subn(pattern, repl, string, count=0, flags=0):
TypeError: expected string or buffer
Shall I check whether of not a particluar tweet is tring before passing it to processtwt() function ? For this error I dont even know which line its failing at.
在将特定推文传递给 processtwt() 函数之前,我是否应该检查它是否不是特定推文?对于这个错误,我什至不知道它在哪一行失败。
回答by d-coder
My answer will be broader than shalini answer. If you want to check if the object is of type strthen I suggest you check typeof object by using isinstance()as shown below. This is more pythonic way.
我的答案将比 shalini 的答案更广泛。如果您想检查对象是否属于类型,str那么我建议您type使用isinstance()如下所示的方法检查对象。这是更pythonic的方式。
tweet = "stackoverflow"
## best way of doing it
if isinstance(tweet,(str,)):
print tweet
## other way of doing it
if type(tweet) is str:
print tweet
## This is one more way to do it
if type(tweet) == str:
print tweet
All the above works fine to check the type of object is string or not.
以上所有方法都可以很好地检查对象的类型是否为字符串。
回答by SIRI RAAVI
Just try using this: tweet = str(tweet).lower()
试试用这个: tweet = str(tweet).lower()
Lately I've been facing many of these errors and converting them to string before applying lower() always worked for me. Thanks!
最近,我遇到了许多这样的错误,并在应用lower() 之前将它们转换为字符串总是对我有用。谢谢!

