【问题标题】:'str' object is not callable even not used str in code'str' 对象是不可调用的,即使没有在代码中使用 str
【发布时间】:2019-04-14 13:55:08
【问题描述】:

我收到此错误。如何解决这个问题?请帮帮我。

# Import pandas
import pandas as pd
import csv

# Load csv
#df = pd.read_csv("D:\Harsha\Trading\cm14SEP2018bhav.csv")

# Read in csv file
#for row in csv.reader(open("D:\Harsha\Trading\cm14SEP2018bhav.csv"), delimiter=','):
      #print(row)
#import csv

infile = 'H:\cm09NOV2018bhav.csv'
outfile = 'H:\output_cm09NOV2018bhav.csv'

wfh = open (outfile, 'w')

with open(infile, 'r') as fh:

    reader = csv.DictReader(fh, delimiter=',')
    wfh.write("{},{},{},{},{},{},{}".format("SYMBOL", "OPEN", "HIGH", "LOW", "CLOSE", "ISIN", "TOTTRDQTY", "STATUS"))
    wfh.write("\n")
    for row in reader:
        symbol = row['SYMBOL']
        series = row['SERIES']
        open = row['OPEN']
        high = row['HIGH']
        low = row['LOW']
        close = row['CLOSE']
        last = row['LAST']
        prevclose = row['PREVCLOSE']
        tottrdqty = row['TOTTRDQTY']
        tottrdval = row['TOTTRDVAL']
        timestamp = row['TIMESTAMP']
        totaltrades = row['TOTALTRADES']
        isin = row['ISIN']
        print(low.rstrip())
        if float(high.rstrip()) in [9,25,49,81,121,169,225,289,301,441,529,625]:
            wfh.write("{},{},{},{},{},{},{},{}".format(symbol, open, high, low, close, isin, tottrdqty, "SELL"))
            wfh.write("\n")
        elif float(low.rstrip()) in [9,25,49,81,121,169,225,289,301,441,529,625]:
            wfh.write("{},{},{},{},{},{},{},{}".format(symbol, open, high, low, close, isin, tottrdqty, "BUY"))
            wfh.write("\n")
        elif float(close.rstrip()) in [9,25,49,81,121,169,225,289,301,441,529,625]:
            wfh.write("{},{},{},{},{},{},{},{}".format(symbol, open, high, low, close, isin, tottrdqty, "CLOSE PRICE"))
            wfh.write("\n")
        elif float(open.rstrip()) in [9,25,49,81,121,169,225,289,301,441,529,625]:
            wfh.write("{},{},{},{},{},{},{},{}".format(symbol, open, high, low, close, isin, tottrdqty, "OPEN PRICE"))
            wfh.write("\n")

#wfh._archive.close()
wfh.close()

我没有在我的代码中使用 str 。为什么会出现此错误?

【问题讨论】:

  • 我认为这不是您的完整代码;对我来说,python 只是试图编译它就会抛出一个语法错误,并且以 with open(infile, 'r') as fh: 结尾看起来是错误的;请提供minimal, complete, and verifiable example
  • 乞求和大喊而不是阅读 cmets 和改进您的问题无济于事。将代码格式化为可读并发布错误消息。很可能在某个时候你已经完成了str = 'something' 并覆盖了str 的默认值。重新启动编辑器。
  • 马克,我不是对任何人大喊大叫,也不是在乞求。我正在寻求前辈的指导来解决这个 str 可调用错误。我的整个代码中都没有包含 str 。那为什么我会收到这个错误?
  • @kamalsharma 通常产生此错误的方式是,如果您执行以下操作:a = 'hello'; print(a()); - 如果您将 str 类型对象分配给变量,然后尝试“调用”该对象好像它是一个函数。您在此代码中使用了大量字符串,但我没有看到任何会导致此错误的内容。如果您用字符串对象覆盖内置函数(或您自己的函数之一),然后尝试调用它,也是可能的。

标签: python-3.x pandas csv


【解决方案1】:

我相信当您使用格式打印时,您应该避免在格式参数中添加字符串值

所有写入 wfh 文件的代码如下:

wfh.write("{},{},{},{},{},{},{},{}".format(symbol, open, high, low, close, isin, tottrdqty, "SELL"))

需要改成:

wfh.write("{},{},{},{},{},{},{},SELL".format(symbol, open, high, low, close, isin, tottrdqty))
# here 'Sell' is a part of the print argument which is already under quotes.

【讨论】:

    【解决方案2】:

    找到了:

            open = row['OPEN']
    

    这段代码使用row['OPEN'] 的内容重新分配python 关键字open(用于打开文件),这意味着现在当您调用open 时,您调用的是str 对象,而不是函数。

    考虑一下:

    print(type(open))
    # <class 'builtin_function_or_method'>
    open = 'hello, world'
    print(type(open))
    # print(type('hello, world'))
    

    您不能再“调用”打开(即open('file.txt', 'r')),因为它是一个字符串,而不是一个函数。

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 2022-01-08
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      • 2014-06-04
      • 2020-11-11
      相关资源
      最近更新 更多