Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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 - Fatal编程技术网

Python 如何添加多个编解码器。打开至';对于';环

Python 如何添加多个编解码器。打开至';对于';环,python,Python,我需要这些线进入“for”循环,以缩短整个模块 peanut = codecs.open("butter.txt", mode="w") duck = codecs.open("tape.txt", mode="w") hair = codecs.open("style.txt", mode="w") italy = codecs.open("spaghetti.txt", mode="w") smile = codecs.open("cheese.txt", mode="w") 比如: fo

我需要这些线进入“for”循环,以缩短整个模块

peanut = codecs.open("butter.txt", mode="w")
duck = codecs.open("tape.txt", mode="w")
hair = codecs.open("style.txt", mode="w")
italy = codecs.open("spaghetti.txt", mode="w")
smile = codecs.open("cheese.txt", mode="w")
比如:

for five_txt in peanut, duck, hair, italy, smile:
    codecs.open()

将文件名放入一个列表中,并对其进行迭代

filenames = ["butter.txt", 
    "tape.txt", 
    "style.txt", 
    "spaghetti.txt", 
    "cheese.txt"]

for fname in filenames:
    fhandler = codecs.open(fname, mode="w")

将文件名放入一个列表中,并对其进行迭代

filenames = ["butter.txt", 
    "tape.txt", 
    "style.txt", 
    "spaghetti.txt", 
    "cheese.txt"]

for fname in filenames:
    fhandler = codecs.open(fname, mode="w")
您现在还可以从字典中访问实例,如:

inst_dict['peanut']
inst_dict['duck']
....
您现在还可以从字典中访问实例,如:

inst_dict['peanut']
inst_dict['duck']
....

谢谢Shashank Gupta谢谢Shashank Gupta谢谢juankysmith,我能用绿色标志标出你的两条评论吗?谢谢juankysmith,我能用绿色标志标出你的两条评论吗?谢谢Ankur Ankan;-)调用dictionary
dict
是个坏主意,因为这会隐藏内置构造函数/类型。@abarnert:谢谢你的建议。我刚刚修改了你的建议,发现它创建这些文件的速度快了3秒,很抱歉耽搁了我睡觉,直到现在才尝试你的建议。非常感谢其他人参与我的帖子,并为我的问题发布了不同的解决方案。谢谢安库·安康和其他人;-)谢谢你,安库·安康;-)调用dictionary
dict
是个坏主意,因为这会隐藏内置构造函数/类型。@abarnert:谢谢你的建议。我刚刚修改了你的建议,发现它创建这些文件的速度快了3秒,很抱歉耽搁了我睡觉,直到现在才尝试你的建议。非常感谢其他人参与我的帖子,并为我的问题发布了不同的解决方案。谢谢安库·安康和其他人;-)