简体   繁体   English

TypeError:字符串索引必须是整数,而不是Python字典上的str

[英]TypeError: string indices must be integers, not str on Python Dictionary

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 must be integers, not str TypeError:字符串索引必须是整数,而不是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()

If I replace Offset with the name of the dictionary so the line reads as follows: 如果我将Offset替换为字典的名称,那么该行的内容如下:

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

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. 然后输出正是我想要的,并且我知道在运行文件时Offset等于E ,因为我添加了一行以将Offset的值打印到控制台窗口。

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? 当它与从“ 方向”列表中获取的变量Offset相同时 ,为什么不能识别字典名称? 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. 这段代码来自嵌套的for循环,因此我需要能够引用列表和字典来获取值,而不是更手动的选择。

You are using subscription syntax on the Offset variable, which is a string , taken from Direction . 您正在对Offset变量(是一个字符串)使用订阅语法,该变量取自Direction

You cannot use the string in Offset as a placeholder for the variable with the same name directly. 您不能将Offset的字符串直接用作具有相同名称的变量的占位符。 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'}
}

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']

暂无
暂无

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

相关问题 typeerror字符串索引必须是整数,而不是str python - typeerror string indices must be integers not str python TypeError:字符串索引必须是整数,而不是python中的str - TypeError: string indices must be integers, not str in python TypeError:尝试创建字典时,字符串索引必须是整数,而不是str - TypeError: string indices must be integers, not str , when trying to create a dictionary TypeError:列表索引必须是整数或切片,而不是str字典python - TypeError: list indices must be integers or slices, not str dictionary python 当尝试从python字典中获取项时,“TypeError:字符串索引必须是整数,而不是str” - “TypeError: string indices must be integers, not str” when trying to get item from python dictionary Python和网络X中的错误-TypeError:字符串索引必须是整数,而不是str - Error in Python and network X - TypeError: string indices must be integers, not str Python、Flask - 类型错误:字符串索引索引必须是整数或切片,而不是字符串 - Python, Flask - TypeError: string index indices must be integers or slices, not str Python字典调用字典,但TypeError:字符串索引必须为整数 - Python Dictionary calls Dictionary but TypeError: string indices must be integers TypeError:字符串索引必须是整数,而不是str-OpenErp - TypeError: string indices must be integers, not str - OpenErp Scrapy:TypeError:字符串索引必须是整数,而不是str? - Scrapy: TypeError: string indices must be integers, not str?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM