Python - 轻松将文本文件内容转换为字典值/键
假设我有一个包含以下内容的文本文件:
line = "this is line 1"
line2 = "this is the second line"
line3 = "here is another line"
line4 = "yet another line!"
我想将它们快速转换为字典键/值,其中“ line* ”为键,引号中的文本为值,同时删除等号。
在 Python 中执行此操作的最佳方法是什么?
Let's say I have a text file with the following:
line = "this is line 1"
line2 = "this is the second line"
line3 = "here is another line"
line4 = "yet another line!"
And I want to quickly convert these into dictionary keys/values with " line* " being the key and the text in quotes as the value while also removing the equals sign.
What would be the best way to do this in Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
希望这有帮助
Hope this helps
一行:
In one line:
InspectorG4dget 的
urlopen
版本的答案可能如下所示:Here's what the
urlopen
version of inspectorG4dget's answer might look like: