小编Con*_*ett的帖子

将python字典转换为大写字母

由于某种原因,我的代码拒绝转换为大写,我无法弄清楚为什么.我试图将字典写入一个文件,其中大写字典值被输入到一种模板文件中.

#!/usr/bin/env python3
import fileinput
from collections import Counter


#take every word from a file and put into dictionary
newDict = {}
dict2 = {}
with open('words.txt', 'r') as f:
        for line in f:
            k,v = line.strip().split(' ')
            newDict[k.strip()] = v.strip()
print(newDict)
choice = input('Enter 1 for all uppercase keys or 2 for all lowercase, 3 for capitalized case or 0 for unchanged \n')
print("Your choice was " + choice)

if choice == 1:
    for k,v in newDict.items():
        newDict.update({k.upper(): v.upper()}) …
Run Code Online (Sandbox Code Playgroud)

python file-io dictionary file python-3.x

0
推荐指数
1
解决办法
1335
查看次数

标签 统计

dictionary ×1

file ×1

file-io ×1

python ×1

python-3.x ×1