【问题标题】:Python : I'm unable to fix TypeError: 'str' object is not callablePython:我无法修复 TypeError:'str' 对象不可调用
【发布时间】:2020-05-16 15:17:59
【问题描述】:

我在 python 中有这段代码:

from ipywidgets import widgets
from IPython.display import display
import os
import pandas as pd

Server = os.listdir('/home/tim/Bureau/Servers/')
ServerList = widgets.Dropdown(options = (Server))

Date = ['2019-10', '2019-11', '2019-12', '2020-01']
DateList = widgets.Dropdown(options = (Date))

display(ServerList,DateList)

Test = os.listdir('/home/tim/Bureau/Servers'+ '/'+ ServerList.value + '/'+ DateList.value+'/')

Path = ('/home/tim/Bureau/Servers'+ '/'+ ServerList.value + '/'+ DateList.value+'/' + str(Test).strip("[]").strip("''") )
display(Path)

df = pd.read_csv(Path)
display(df)

结果是:

但我有这个错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-114-2664737bd451> in <module>
     17 display(Path)
     18 
---> 19 df = pd.read_csv(Path)
     20 display(df)
     21 

TypeError: 'str' object is not callable

我知道我使用了 strstr(Test),但这是我找到的唯一解决方案,我不知道如何解决此错误。

我想恢复路径以放入 pd.read_csv 以显示 csv。 可以给我看看吗?

【问题讨论】:

  • 最好使用os.path.join()创建路径名而不是字符串连接。
  • print(type(pd.read_csv)) 是否返回 str
  • 这真的是整个剧本吗?您已经完成了将 pd.read_csv 函数替换为字符串的操作。
  • 大家好!事实上,我还有另一个str。谢谢!

标签: python string pandas


【解决方案1】:

我认为您忘记将 df 转换为 pandas 数据框:

df = pd.DataFrame(数据)

【讨论】:

  • 他的脚本中没有数据变量。此外,没有必要这样做,因为 pd.read_csv() 方法已经返回了一个数据框:)
猜你喜欢
  • 1970-01-01
  • 2015-02-15
  • 2020-09-16
  • 1970-01-01
  • 1970-01-01
  • 2013-08-05
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多