简体   繁体   English

字符串索引必须是python的整数

[英]String indices must be integers, new to python

I hope I won't get trashed for this question. 我希望我不会被这个问题所困扰。 This is my first day with Python and all I've done so far is copy pasting and deducing from other snippets of code. 这是我使用Python的第一天,到目前为止,我所做的就是复制粘贴并从其他代码片段中推导出来。 I have no experience with code. 我没有代码经验。 I'm trying my hardest however this one I can't get past for the past few hours. 我正在尽我最大的努力,但是这是我过去几个小时无法克服的事情。

I'm currently adjusting an Editorial (iOS app) workflow to fit my needs — namely: posting to my WordPress site, including the ability to choose from a list of all possible post formats (I have "standard" and "link" enabled). 我目前正在调整“编辑”(iOS应用)工作流程以适应我的需要,即:发布到我的WordPress网站,包括从所有可能的发布格式列表中进行选择的功能(我启用了“标准”和“链接”) 。

Here is the faulty bit from the relevant Python script: 这是相关的Python脚本中的错误位:

console.show_activity('Fetching formats...')

server = xmlrpclib.ServerProxy(wp_url)

format = server.wp.getPostFormats(wp_blogid, wp_username, wp_password, 'post_format')


i = 0
for x in format:
        formats += '\n' + x['name'] + " " + str(i)
        i = i+1

workflow.set_variable("formats", formats)
console.hide_activity()

I'm getting the error: "string indices must be integers, not str" 我收到错误消息:“字符串索引必须是整数,而不是str”

What is this supposed to do is later show me in a list my available formats. 这应该做什么,以后再以可用格式列出我。 I've done this successfully with server.wp.getPostFormats(wp_blogid, wp_username, wp_password, 'post_tag') and server.wp.getTerms(wp_blogid, wp_username, wp_password, 'category') 我已经使用server.wp.getPostFormats(wp_blogid, wp_username, wp_password, 'post_tag')server.wp.getTerms(wp_blogid, wp_username, wp_password, 'category')成功完成了此操作

I know my problem is where the line with the i starts, I just have no idea how to solve it. 我知道我的问题是i起点,我只是不知道如何解决。 I'm assuming by reading other topics that I need to cast, but I'm not even sure what getPostFormats returns, or how to do that. 我假设通过阅读我需要转换的其他主题,但是我什至不确定getPostFormats返回什么或如何执行。

Thanks in advance! 提前致谢!

Edit: I've now concluded that server.wp.getPostFormats returns a dictionary, but I have not a slightest idea on how to retrieve only one set of data (either the key or value) of this dictionary. 编辑:我现在得出结论, server.wp.getPostFormats返回一个字典,但是我对如何仅检索该字典的一组数据(键或值)没有丝毫想法。 Please help. 请帮忙。

You're getting that error because "string indices must be integers, not str". 您会收到该错误,因为“字符串索引必须是整数,而不是str”。

    formats += '\n' + x['name'] + " " + str(i)

Here, you are accessing the 'name'th element of x , where 'name' is of course a string and x is a string too, since server.wp.getPostFormats obviously returns a list of strings. 在这里,您正在访问x 'name'元素,其中'name'当然是一个字符串,x也是一个字符串,因为server.wp.getPostFormats很明显返回了一个字符串列表。

So simply look at format and you should immediately see how to obtain the required data. 因此,只需看一下format ,您就应该立即看到如何获取所需的数据。

UPDATE: 更新:

OK, so you figured out that format is a dictionary (returned from server.wp.getPostFormats ). 好的,所以您确定format是字典(从server.wp.getPostFormats返回)。 In Python, if you iterate over a dictionary (your for-loop), you iterate through its keys. 在Python中,如果您遍历字典(for循环),则遍历其键。 This is the standard behaviour. 这是标准行为。

Look at this example: 看这个例子:

>>> foo = {'a': 1, 'c': 3, 'b': 2}
>>> for x in foo: print(x)
... 
a
c
b

Of course you also need the values, not only the keys (you seem to look for an entry with the key name ). 当然,您不仅需要键,而且还需要值(您似乎在寻找具有键name的条目)。 One way is to iterate though the keys and items in one shot: 一种方法是遍历一次镜头中的键和项:

>>> for key, value in foo.iteritems():
...     print(key + ":" + str(value))
... 
a:1
c:3
b:2

So the .iteritems() method of a dictionary returns for every iteration a key/value-pair, which you can unpack within the for loops definition. 因此,字典的.iteritems()方法为每次迭代返回一个键/值对,您可以在for循环定义中解压缩该键/值对。

In your case, you can do the following: 您可以执行以下操作:

for key, value in format.iteritems():
    formats += 'key: ' + key + ', value: ' + value + '\n'

Given that you are new, I would also suggest using the enumerate method and refactoring your code: 如果您是新手,我还建议您使用enumerate方法并重构您的代码:

for i, x in enumerate(format):
    formats += '\n' + x[idx_for_name] + " " + str(i)

The idx_for_name would be the index for the name. idx_for_name将是名称的索引。

If you wanna go even further, you can use the join method too: 如果您想走得更远,也可以使用join方法:

formats = '\n'.join(x[idx_for_name] + " " + str(i) \
                        for i, x in enumerate(format))

warning : This code is untested. 警告 :此代码未经测试。

Read: http://python-wordpress-xmlrpc.readthedocs.org/en/latest/ref/methods.html#wordpress_xmlrpc.methods.posts.GetPostFormats 阅读: http : //python-wordpress-xmlrpc.readthedocs.org/en/latest/ref/methods.html#wordpress_xmlrpc.methods.posts.GetPostFormats

GetPostFormats return a dictionary so x iterates among the keys which are strings. GetPostFormats返回一个字典,因此x在作为字符串的键之间进行迭代。

Try: 尝试:

x= format['all']:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM