python - 分区和 rpartiton 出现 TypeError : 'builtin_function_or_method' object has no attribute '__getitem__'

标签 python partition

当我尝试运行脚本时收到此错误。

TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'

这是脚本:

containerFile = open((tmpImageDirectory+"container.rvbr"), "r")
containedString = containerFile.read()
containerFile.close()
containedFiles = containedString .partition[","]

container.rvbr 包含一个带有多个逗号的字符串。 如果执行这个str(conatinedString)我明白了<type 'str'> ,所以它是一个字符串。我想知道是否有人可以解释这一点。

最佳答案

使用括号而不是方括号来调用方法:

containedFiles = containedArray.partition(",")

此外,您可能想要进行 split 而不是 partition:partition 会将逗号保留在结果元组中,即使这些逗号实际上不是文件:

"hello,world".partition(",")
# ('hello', ',', 'world')
"hello,world".split(",")
# ['hello', 'world']

关于python - 分区和 rpartiton 出现 TypeError : 'builtin_function_or_method' object has no attribute '__getitem__' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19525087/

相关文章:

python - 扩展算法所需的概念和工具

Python 分区依据

sql - 根据 SQL 条件分配季度数和年份数

math - 按词汇顺序查找总和为给定数字的千组

linux - 如何自动分配剩余的空闲空间给分区?

python - 通过conda安装特定版本的spyder

python - 使用适用于 python 3.4 的 anaconda 3 在 ubuntu 15.04 上安装 caffe - 找不到模块 caffe

python - 在pygame<外星人入侵>中,为什么我的外星人只出现一排?

python - 使用 Python 更改网格厚度的极坐标图

apache-kafka - 我可以删除 Kafka 分区版本 0.10.0.1