python - 尝试从 python 中的字典中拆分作为键的字符串,出现无法解释的错误。为什么?

标签 python string dictionary

出于某种原因,我在 Python 中的这些代码行上遇到了错误:

for identity in identities.iterkeys():
    if "twitter" in identity: 
        OUT.write(identity.rsplit('/', 0))

这是我收到的错误:

OUT.write(identity.rsplit('/', 0))
TypeError: must be string or buffer, not list

我完全困惑为什么 if "twitter" 行可以工作,但 rsplit 不行,而且我整晚都在绞尽脑汁地反对这个问题。 (该字符串是一个 URL,如果有什么区别的话。)

最佳答案

rsplit 返回一个列表。您需要再次检索该字符串。

OUT.write(identity.rsplit('/', 0)[0])

顺便说一句,identity.rsplit('/', 0')[0] 只是返回您的 identity 字符串,未更改。也许您正在寻找 identity.rsplit('/', 1)[1]

关于python - 尝试从 python 中的字典中拆分作为键的字符串,出现无法解释的错误。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9249717/

相关文章:

c++ - C++ 中指向整数映射和查找的高效指针

带有复合键的 Javascript 映射

python - 全局名称 're' 未定义

python - 无法安装pyspark

python - 将pyspark数据帧转换为python字典列表

c - C 中的多行输入 - 疑难解答

c# - ASP.NET GridView 删除 'Input string was not in a correct format'

C++ c2664 错误 "cannot convert argument 1 from std::string to _Elem *"

java - 一个并发集合位于另一个集合中 : is it thread safe

python - 在pygame中获取图像单个像素的颜色