简体   繁体   English

我可以将文本文件转换为python字典吗?

[英]Can I convert a text file to python dictionary?

d = dict((line.strip().split(' = ') for line in file(filename)))

我使用了这段代码,但是打印出错误。

I think there is a mistake in your code. 我认为您的代码中存在错误。

Simply replace file with open like this: 只需用open file就像这样:

d = dict((line.strip().split(' = ') for line in open(filename)))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM