对于for循环中的open,TypeError:“str”对象不可调用(Python)

2024-10-02 04:22:17 发布

您现在位置:Python中文网/ 问答频道 /正文

当在一个循环中打开和读取多个文本文件时,我得到一个str not callable错误,开始第二次/open。不幸的是,我无法独自解决这个问题。请看我的代码:

for symbol in sorted(list(symbollist.keys())):  
    textfile = symbol+"."+symbollist[symbol]['Country']+".txt"
    textfile = textfile.lower()
    index = symbollist[symbol]['Index']
    country = symbollist[symbol]['Country']
    ticker = symbol
    name = symbollist[symbol]['Name']
    if index not in data:
        data[index] = {}
    if country not in data[index]:
        data[index][country] = {}
    if ticker not in data[index][country]:
        data[index][country][ticker] = {}
    if name not in data[index][country][ticker]:
        data[index][country][ticker][name] = {}
    indexaddress = "d_all_txt\\data\\daily\\us\\nasdaq stocks\\1+2\\" 
    textad = (indexaddress+textfile)
    print(textad)
    with open(textad) as s:
        print("test")   
        next(s)
        for line in s: 

输出:

^{pr2}$

我尝试使用以下方法单独/手动打开这两个文件:

textad = "d_all_txt\\data\\daily\\us\\nasdaq stocks\\1+2\\aaap.us.txt"

或者

textad = "d_all_txt\\data\\daily\\us\\nasdaq stocks\\1+2\\aaba.us.txt"

只有当代码第二次出现在with open语句(在for符号循环中)时,才会出现问题

希望有人能帮我解决这个问题!在


Tags: intxtfordataindexifnotopen
1条回答
网友
1楼 · 发布于 2024-10-02 04:22:17

您将open重新分配给内部循环其余部分中的某个字符串。

相关问题 更多 >

    热门问题