【问题标题】:Bugs un-understandable: var considered sometimes as list and sometimes as str无法理解的错误:var 有时被视为列表,有时被视为 str
【发布时间】:2018-03-29 14:21:36
【问题描述】:

这是一个非常奇怪的错误,我在 Python 上根本无法理解。 下面的说明详细说明了该函数应执行的操作。

f 和 f2 的创作和写作运作良好。我用一个例子来测试这个函数:histo_appels(['bob','jean','bob'],['21/03','22/03','28/03]).

Python 给了我一个错误信息:

line = line.split() // 列表对象没有属性'split'。

但在我的函数中,line 应该是从 f 中提取的字符串。

意思是在我做line = line.split()之前,line已经是一个列表了。那么好吧,我直接尝试做line.extend(line2),这次Python返回错误信息:

line.extend(line2) AttributeError: 'str' object has no attribute 'extend'

这意味着这一次 line 是一个字符串,而之前它被称为一个列表 ...

有什么想法吗?

def histo_appels(liste_contact, liste_date):
        liste = []
        s = set(liste_contact)
        l = list(s)
        # we identify the contact that try to call me 
        # set to remove doubles: ['bob','jean','bob'] becomes ['bob','jean']
        chaine = str(liste_contact)
        # count() can only be applied to a string

        f = open(r'c:\temp\historique_contact.txt','w',encoding='utf8')
        for i in range(len(s)):
            liste.append((l[i],chaine.count(l[i])))
            f.write(f"{l[i]} tried to call you {chaine.count(l[i])} time\n")
        f.close()
        # example : line 1 of f contains "bob tried to call you 2 time", 
        # and line 2 : "jean tried to call you 1 time"

        f2 = open(r'c:\temp\historique_date.txt','w',encoding='utf8')
        for i in s:
            liste1 = []
            for j in range(len(liste_contact)):
                if liste_contact[j] == i:
                    liste1.append(liste_date[j])
            f2.write(f"on the {liste1}\n")
            # example line 1 of f2 is "on the ['21/03','28/03']"
            # and line 2 of f2 is : "on the ['22/03']
        f2.close()

        # then we merge each line of f with f2 in a new file f3
        f = open(r'c:\temp\historique_contact.txt','r',encoding='utf8')
        f2 = open(r'c:\temp\historique_date.txt','r',encoding='utf8')
        f3 = open(r'c:\temp\historique_global.txt','w',encoding='utf8')
        for line in f:
            for line2 in f2:
                line = line.split()
                # on repasse line (=str) en liste
                line2 = line2.split()
                line.extend(line2)
                f3.write(" ".join(line))
        f3.close()
        f2.close()
        f.close()

【问题讨论】:

  • 您在一个循环中多次在同一个line 上运行line = line.split()。无论你想做什么,这都不是办法。

标签: python string list file attributes


【解决方案1】:

问题出在嵌套的for循环中:

for line in f:
    for line2 in f2:
        line = line.split()
        # on repasse line (=str) en liste
        line2 = line2.split()
        line.extend(line2)
        f3.write(" ".join(line))

当您进入第二个for 循环时,您将不断拆分line,重新分配他的值。我不明白你想做什么,但这个具体问题已经解决了 py 在正确的 for 循环中调用 line 上的 split

for line in f:
    line = line.split()
    for line2 in f2:
        # on repasse line (=str) en liste
        line2 = line2.split()
        line.extend(line2)
        f3.write(" ".join(line))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    相关资源
    最近更新 更多