TypeError:string indices必须是整数,而不是Python Dictionary上的str [英] TypeError: string indices must be integers, not str on Python Dictionary

查看:903
本文介绍了TypeError:string indices必须是整数,而不是Python Dictionary上的str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被卡住了,我确信是简单的,但我现在只是圈出来。以下代码是一个脚本的代码片段,它遍历了我输入到列表和字典中的一些值,并生成了可以进入另一个程序的文本文件。我遇到的问题是当尝试循环方向列表并将相应的值从相关字典打印到文件时出现以下错误:

I'm stuck, on what I'm sure is something simple, but I'm just going around in circles now. The following code is a snippet of a script that iterates over some values I've entered into lists and dictionaries and produces text files that I can feed into another program. The problem I'm having is when attempting to loop round the Direction list and have corresponding values from the relevant dictionary print to file I get the following error:

TypeError:string indices必须是整数,而不是str

TypeError: string indices must be integers, not str

Direction = ('E', 'NE')

E = {
    'InitialHeading': 0,
    'InitialX': 22.480,
    'InitialY': 0.000,
    'ActiveCurrent': '10y_Current_W'}

NE = {
    'InitialHeading': 45,
    'InitialX': 15.896,
    'InitialY': 15.896,
    'ActiveCurrent': '10y_Current_SW'}

casenumber = 0

for Offset in Direction:

            # CREATE INDIVIDUAL TEXT FILES
            casenumber = casenumber + 1
            filename = 'Case%.3d.txt' % casenumber
            f = open(filename, 'w')
            print >>f, 'InitialHeading: ', Offset['InitialHeading']
            print >>f, 'InitialX: ', Offset['InitialX']
            print >>f, 'InitialY: ', Offset['InitialY']
            print >>f, 'ActiveCurrent: ', Offset['ActiveCurrent']
            f.close()

如果我用字典的名称替换 Offset ,这样行如下:

If I replace Offset with the name of the dictionary so the line reads as follows:

print >>f, 'InitialHeading: ', E['InitialHeading']

然后输出正是我想要的,我知道当我运行文件时,偏移量等于 E ,因为我添加了一行来打印 Offset 到控制台窗口。

Then the output is exactly what I want and I know that Offset is equal to E when I run the file, as I added a line to print the value of Offset to the console window.

为什么当它与变量的偏移值相同时才会识别字典名称? em>,从方向列表中获取?这段代码来自嵌套for循环,所以我需要能够参考列表&字典获得这些值,而不是一个更加手动的替代方案。

Why doesn't it recognise the dictionary name when it's the same value as the variable Offset, which is obtained from the Direction list? This piece of code is from nested for loops, so I need to be able to refer to the lists & dictionaries to obtain the values, rather than a more manual alternative.

推荐答案

您正在使用订阅语法偏移变量,这是一个字符串,取自 Direction

You are using subscription syntax on the Offset variable, which is a string, taken from Direction.

您不能将 Offset 中的字符串直接用作具有相同名称的变量的占位符。相反,*存储字典:

You cannot use the string in Offset as a placeholder for the variable with the same name directly. Instead, *store the dictionary:

E = {
    'InitialHeading': 0,
    'InitialX': 22.480,
    'InitialY': 0.000,
    'ActiveCurrent': '10y_Current_W'}

NE = {
    'InitialHeading': 45,
    'InitialX': 15.896,
    'InitialY': 15.896,
    'ActiveCurrent': '10y_Current_SW'}

Direction = (E, NE)

或更好的是,使用另一个字典来包装路线:

or better still, use another dictionary to wrap the directions:

Direction = {
    'E': {
        'InitialHeading': 0,
        'InitialX': 22.480,
        'InitialY': 0.000,
        'ActiveCurrent': '10y_Current_W'},

    'NE': {
        'InitialHeading': 45,
        'InitialX': 15.896,
        'InitialY': 15.896,
        'ActiveCurrent': '10y_Current_SW'}
}

现在你可以循环ove r 那个字典,并且具有方向的字符串名称和与之相关联的设置:

Now you can loop over that dictionary and have both a string name for the direction and the settings associated with it:

for direction, settings in Directions.items():
    # CREATE INDIVIDUAL TEXT FILES
    casenumber = casenumber + 1
    filename = 'Case%.3d.txt' % casenumber
    with open(filename, 'w') as f:
        print >>f, 'InitialHeading: ', settings['InitialHeading']
        print >>f, 'InitialX: ', settings['InitialX']
        print >>f, 'InitialY: ', settings['InitialY']
        print >>f, 'ActiveCurrent: ', settings['ActiveCurrent']

这篇关于TypeError:string indices必须是整数,而不是Python Dictionary上的str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆