python - 无法将 'list' 对象隐式转换为 str python

标签 python string list

elif used_prefix and cmd == "xp":
            if self.getAccess(user) >= 1:
                f = open("users/" + user.name.lower() + ".txt", 'r')
                word = f.readline().split("X Points = ")
                if word == "0":
                        room.message("You have no X Points")
                else:
                    room.message("You Have " + word + " X Points")
                f.close()
            else:
                room.message("You are not whitelisted " + user.name.capitalize())

当我尝试使用 XP 时,它在控制台中显示 Can't conversion 'list' object to strimplicitly 错误。我正在使用 python 3.3。

最佳答案

你可能需要

word = f.readline().split("X Points = ")[1].strip()

当您拆分时,它将返回拆分为列表的项目列表。需要取实际值对应的元素

示例

data = "X Points = 10"
print data.split("X Points = ")

输出

['', '10']

所以,我们需要获取第二个元素。这就是我们使用 [1]

的原因

关于python - 无法将 'list' 对象隐式转换为 str python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20374011/

相关文章:

python - 使用 Python API 在 Maya 中获取引用节点列表

python - 更新 numpy 数组中的值,无需繁琐的 for 循环

如果我们必须将多个由空格组成的字符串作为输入,我们可以使用 scanf("%[^\n]s,str) 吗?

list - 在 Haskell 中表示 2D 矢量的正确方法是什么?

python - 如何将列表转换为具有多列的数据框?

python - Django: NotImplementedError: annotate() + distinct(fields) 未实现

python - Dijkstra + 堆(自行实现)

Python:我有一个字符串和一个长度不一的列表列表,我想按组返回与列表相对应的字母

c# - 对于 var q = "A"+ "B"+ "C",字符串连接在 C# 中是如何发生的

c# - 在 C# 中使用 LINQ 比较两个 List<string>