鱼C论坛

 找回密码
 立即注册
查看: 11160|回复: 1

怎样将一个TXT文件转换成字典的形式,可以让python读取

[复制链接]
发表于 2015-12-31 15:42:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
怎样将一个TXT文件转换成字典的形式,可以让python读取
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-12-31 15:44:28 | 显示全部楼层
  1. def get_userinfo(path):
  2.     user_info = []
  3.     config = codecs.open(path,'r','utf-8')
  4.     for line in config:
  5.         user_dict = {}
  6.         result = [ele.strip() for ele in line.split(";")]/易;分割
  7.         for r in result:
  8.             account = [ele.strip() for ele in r.split('=')]
  9.             print(account)
  10.             user_dict.update(dict([account]))
  11.         user_info.append(user_dict)
  12.     return user_info

  13. if __name__ == '__main__':
  14.    
  15.     webinfo = get_webinfo(r"路径")
  16.     for key in webinfo:
  17.         print (key,webinfo[key])
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-15 23:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表