Python 问题:TypeError:';浮动';对象不可编辑

Python 问题:TypeError:';浮动';对象不可编辑,python,python-3.x,for-loop,floating-point,Python,Python 3.x,For Loop,Floating Point,我在windows 7上使用python 3.2.2。这是我代码的一部分。它从excel文件读取。但当我运行代码时,它只从0打印到10,并给出“TypeError:“float”对象不可编辑”。 谢谢你的帮助 pages = [i for i in range(0,19634)] for page in pages: x=df.loc[page,["id"]] x=x.values x=str(x)[2:-2] text=df.loc[page,["rev"]] d

我在windows 7上使用python 3.2.2。这是我代码的一部分。它从excel文件读取。但当我运行代码时,它只从0打印到10,并给出“TypeError:“float”对象不可编辑”。 谢谢你的帮助

 pages = [i for i in range(0,19634)]


    for page in  pages:

 x=df.loc[page,["id"]]
 x=x.values
 x=str(x)[2:-2]
 text=df.loc[page,["rev"]]

 def remove_punct(text):
  text=''.join([ch.lower() for ch in text if ch not in exclude])
  tokens = re.split('\W+', text)
  tex = " ".join([wn.lemmatize(word) for word in tokens if word not in stopword])

  removetable = str.maketrans('', '', '1234567890')
  out_list = [s.translate(removetable) for s in tokens1] 
  str_list = list(filter(None,out_list)) 
  line = [i for i in str_list if len(i) > 1]

  return line

 s=df.loc[page,["rev"]].apply(lambda x:remove_punct(x))

 with open('FileNamex.csv', 'a', encoding="utf-8") as f:
     s.to_csv(f, header=False)

 print(s)
这就是错误所在

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-54-c71f66bdaca6> in <module>()
     33   return line
     34 
---> 35  s=df.loc[page,["rev"]].apply(lambda x:remove_punct(x))
     36 
     37  with open('FileNamex.csv', 'a', encoding="utf-8") as f:

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
   3190             else:
   3191                 values = self.astype(object).values
-> 3192                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   3193 
   3194         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/src\inference.pyx in pandas._libs.lib.map_infer()

<ipython-input-54-c71f66bdaca6> in <lambda>(x)
     33   return line
     34 
---> 35  s=df.loc[page,["rev"]].apply(lambda x:remove_punct(x))
     36 
     37  with open('FileNamex.csv', 'a', encoding="utf-8") as f:

<ipython-input-54-c71f66bdaca6> in remove_punct(text)
     22 
     23  def remove_punct(text):
---> 24   text=''.join([ch.lower() for ch in text if ch not in exclude])
     25   tokens = re.split('\W+', text)
     26   tex = " ".join([wn.lemmatize(word) for word in tokens if word not in stopword])

TypeError: 'float' object is not iterable
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
33回油管
34
--->35 s=df.loc[第页[“修订版]]适用(lambda x:移除点(x))
36
37打开('FileNamex.csv','a',encoding=“utf-8”)作为f:
C:\ProgramData\Anaconda3\lib\site packages\pandas\core\series.py在apply中(self、func、convert\u dtype、args、**kwds)
3190其他:
3191 values=self.astype(object.values)
->3192 mapped=lib.map\u推断(值,f,convert=convert\u数据类型)
3193
3194如果len(映射)和isinstance(映射[0],系列):
pandas/_-libs/src\inference.pyx在pandas._-libs.lib.map\u-infere()中
in(x)
33回油管
34
--->35 s=df.loc[第页[“修订版]]适用(lambda x:移除点(x))
36
37打开('FileNamex.csv','a',encoding=“utf-8”)作为f:
在删除点(文本)中
22
23 def移除点(文本):
--->24 text=''.join([ch.lower(),如果ch不在exclude中,则表示ch在text中)
25令牌=重新拆分('\W+',文本)
26 tex=”“.join([wn.lemmatize(word)表示代词中的单词,如果单词不在stopword中])
TypeError:“float”对象不可编辑

谢谢你的帮助

您正在尝试应用一个迭代
文本的函数(不管它是什么),并且您使用
浮点值调用它

 pages = [i for i in range(0,19634)]


    for page in  pages:

 x=df.loc[page,["id"]]
 x=x.values
 x=str(x)[2:-2]
 text=df.loc[page,["rev"]]

 def remove_punct(text):
  text=''.join([ch.lower() for ch in text if ch not in exclude])
  tokens = re.split('\W+', text)
  tex = " ".join([wn.lemmatize(word) for word in tokens if word not in stopword])

  removetable = str.maketrans('', '', '1234567890')
  out_list = [s.translate(removetable) for s in tokens1] 
  str_list = list(filter(None,out_list)) 
  line = [i for i in str_list if len(i) > 1]

  return line

 s=df.loc[page,["rev"]].apply(lambda x:remove_punct(x))

 with open('FileNamex.csv', 'a', encoding="utf-8") as f:
     s.to_csv(f, header=False)

 print(s)
float
s无法迭代。您可以先使用
text=str(text)
将任何输入转换为文本-但看看您的代码,我不知道这是否有意义

您可以检查是否正在处理这样的浮动:

def remove_punct(text):

     if isinstance(text,float): 
         pass   #    do something sensible with floats here
         return #    something sensible

     text=''.join([ch.lower() for ch in text if ch not in exclude])
     tokens = re.split('\W+', text)
     tex = " ".join([wn.lemmatize(word) for word in tokens if word not in stopword])

     removetable = str.maketrans('', '', '1234567890')
     out_list = [s.translate(removetable) for s in tokens1] 
     str_list = list(filter(None,out_list)) 
     line = [i for i in str_list if len(i) > 1]

     return line
您可以通过
isinstance
处理
float
,也可以从中获得灵感 关于如何检测您是否提供了任何iterable。您需要以不同的方式处理不可编辑的内容