Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python获取此文件中的变量名_Python_Dictionary - Fatal编程技术网

如何使用python获取此文件中的变量名

如何使用python获取此文件中的变量名,python,dictionary,Python,Dictionary,这是a.txt文件: attackUp = [10, 15] defenceUp = [10, 15] magicUp = [10, 15] attType = [1, 1] weightDown = [10, 15] #装饰器数据 accAttackSword = [100, 100] accAttackSaber = [100, 100] accAttackAx = [100, 100] accAttackHa

这是a.txt文件:

    attackUp = [10, 15]
    defenceUp = [10, 15]
    magicUp = [10, 15]
    attType = [1, 1]
    weightDown = [10, 15]

    #装饰器数据
    accAttackSword = [100, 100]
    accAttackSaber = [100, 100]
    accAttackAx = [100, 100]
    accAttackHammer = [100, 100]
    accAttackSpear = [100, 100]
    accAttackFight = [100, 100]

    accAttackBow = [100, 100]
    accAttackMagicGun = [100, 100]
    accAttackMagic = [100, 100]
    mStrInstrument = [100, 100]
    mStrCharms = [100, 100]
    accDefencePhy = [100, 100]
    accDefenceMag = [100, 100]
    accWeight = [100, 90, 0,0, 100, 90]

    #战术书数据
    bookTurn = [1, 1]
    bookAttackPhy = [100, 100]
    bookAttackMag = [100, 100]
    bookStrInstrument = [100, 100]
    bookStrCharms = [100, 100]
    bookDefencePhy = [100, 100]
    bookDefenceMag = [100, 100]
    bookWeight = [100, 100]

    #数据
    data = self.owner.get_user_info()
    npc = self.create_npc()
    name = [self.owner.name,'npc_1']
    plvl = [data.get('level'),npc.get('level')]
    str = [data.get('power'),npc.get('power')]
    ski = [data.get('skill'),npc.get('skill')]
    mag = [data.get('magic'),npc.get('magic')]
    spd = base.get('speed')[:2]
    locX = base.get('locX')
    locX = [locX[1],locX[4]]
    locY = base.get('locY')
    locY = [locY[1],locY[4]]
    wName = []; wAttack = []; wDefence = []; wWeight = [];wType = []; target = []
我想得到一个这样的口述:

{'attackUp':attackUp,'defenceUp':defenceUp,'magicUp':magicUp ...}
这是我的代码:

a=open('a.txt', 'rb')
a=a.read()
我能做什么

谢谢

更新:

我使用以下代码:

with open('a.txt', 'rb') as f:
    contents = f.read().decode('utf-8-sig')

items = {  }
for line in contents.split('\n'):
    item = line.split('=')[0].strip()
    items[item] = item

b=open('b.txt','wb')
b.write(unicode(items))
但它表明:

{u'': u'', u'accDefencePhy': u'accDefencePhy', u'mStrCharms': u'mStrCharms', ..}
我想去

{ 'accDefencePhy': accDefencePhy, 'mStrCharms': mStrCharms, ..}

那么,如何删除
u

为什么不将该txt文件转换为.py文件,并将其导入到要在其中使用值的模块中呢

而不是

attackup=chardict['attackup']

进口字符


attackup=charattrs.attackup

如果从文件中删除
self.
“变量”(Python不知道如何理解它们),则可以获得一个包含所需内容的字典:

import a  # This supposes that your file is named a.py instead of a.txt

var_dict = vars(a)

print var_dict['accDefencePhy']  # Yields [100, 100]

(您还可以在var_dict(即
\uu doc\uu
等)中获得一些附加条目,在使用
var_dict=vars(a.copy()
复制原始词典后,如果需要,您可以删除这些条目)

您希望dict中的“plvl”是什么。?您的文件似乎包含Python代码……所以您希望字典不包含值,而是包含变量名(但不作为字符串)?这是不可能的。字典只能包含对象。。。除非您的代码的作用域中碰巧有
accdeffencephy
等,并且您希望将它们的值注入到字典中