这是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
答案 0 :(得分:1)
如果从文件中删除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中获得一些其他条目(即__doc__
等),如果需要,可以在使用var_dict = vars(a).copy()
复制原始字典后删除。)
答案 1 :(得分:0)
为什么不将该txt文件转换为.py文件并将其导入要使用其中的值的模块?
而不是
attackup = chardict ['attackup']
导入charattrs
attackup = charattrs.attackup