Python用list或dict字段模式读取文件的方法


Posted in Python onJanuary 10, 2017

前言

Python用于处理文本数据绝对是个利器,极为简单的读取、分割、过滤、转换支持,使得开发者不需要考虑繁杂的流文件处理过程(相对于JAVA来说的,嘻嘻)。博主自己工作中,一些复杂的文本数据处理计算,包括在HADOOP上编写Streaming程序,均是用Python完成。

而在文本处理的过程中,将文件加载内存中是第一步,这就涉及到怎样将文件中的某一列映射到具体的变量的过程,最最愚笨的方法,就是按照字段的下标进行引用,比如这样子:

# fields是读取了一行,并且按照分隔符分割之后的列表
user_id = fields[0]
user_name = fields[1]
user_type = fields[2]

如果按照这种方式读取,一旦文件有顺序、增减列的变动,代码的维护是个噩梦,这种代码一定要杜绝。

本文推荐两种优雅的方式来读取数据,都是先配置字段模式,然后按照模式读取,而模式则有字典模式和列表模式两种形式;

读取文件,按照分隔符分割成字段数据列表

首先读取文件,按照分隔符分割每一行的数据,返回字段列表,以便后续处理。

代码如下:

def read_file_data(filepath):
 '''根据路径按行读取文件, 参数filepath:文件的绝对路径
 @param filepath: 读取文件的路径
 @return: 按\t分割后的每行的数据列表
 '''
 fin = open(filepath, 'r')
 for line in fin:
  try:
   line = line[:-1]
   if not line: continue
  except:
   continue
  
  try:
   fields = line.split("\t")
  except:
   continue
  # 抛出当前行的分割列表
  yield fields
 fin.close()

使用yield关键字,每次抛出单个行的分割数据,这样在调度程序中可以用for fields in read_file_data(fpath)的方式读取每一行。

映射到模型之方法1:使用配置好的字典模式,装配读取的数据列表

这种方法配置一个{“字段名”: 字段位置}的字典作为数据模式,然后按照该模式装配读取的列表数据,最后实现用字典的方式访问数据。

所使用的函数:

@staticmethod
def map_fields_dict_schema(fields, dict_schema):
 """根据字段的模式,返回模式和数据值的对应值;例如 fields为['a','b','c'],schema为{'name':0, 'age':1},那么就返回{'name':'a','age':'b'}
 @param fields: 包含有数据的数组,一般是通过对一个Line String通过按照\t分割得到
 @param dict_schema: 一个词典,key是字段名称,value是字段的位置;
 @return: 词典,key是字段名称,value是字段值
 """
 pdict = {}
 for fstr, findex in dict_schema.iteritems():
  pdict[fstr] = str(fields[int(findex)])
 return pdict

有了该方法和之前的方法,可以用以下的方式读取数据:

# coding:utf8
"""
@author: www.crazyant.net
测试使用字典模式加载数据列表
优点:对于多列文件,只通过配置需要读取的字段,就能读取对应列的数据
缺点:如果字段较多,每个字段的位置配置,较为麻烦
"""
import file_util
import pprint
 
# 配置好的要读取的字典模式,可以只配置自己关心的列的位置
dict_schema = {"userid":0, "username":1, "usertype":2}
for fields in file_util.FileUtil.read_file_data("userfile.txt"):
 # 将字段列表,按照字典模式进行映射
 dict_fields = file_util.FileUtil.map_fields_dict_schema(fields, dict_schema)
 pprint.pprint(dict_fields)

输出结果:

{'userid': '1', 'username': 'name1', 'usertype': '0'}
{'userid': '2', 'username': 'name2', 'usertype': '1'}
{'userid': '3', 'username': 'name3', 'usertype': '2'}
{'userid': '4', 'username': 'name4', 'usertype': '3'}
{'userid': '5', 'username': 'name5', 'usertype': '4'}
{'userid': '6', 'username': 'name6', 'usertype': '5'}
{'userid': '7', 'username': 'name7', 'usertype': '6'}
{'userid': '8', 'username': 'name8', 'usertype': '7'}
{'userid': '9', 'username': 'name9', 'usertype': '8'}
{'userid': '10', 'username': 'name10', 'usertype': '9'}
{'userid': '11', 'username': 'name11', 'usertype': '10'}
{'userid': '12', 'username': 'name12', 'usertype': '11'}

映射到模型之方法2:使用配置好的列表模式,装配读取的数据列表

如果需要读取文件所有列,或者前面的一些列,那么配置字典模式优点复杂,因为需要给每个字段配置索引位置,并且这些位置是从0开始完后数的,属于低级劳动,需要消灭。

列表模式应命运而生,先将配置好的列表模式转换成字典模式,然后按字典加载就可以实现。

转换模式,以及用按列表模式读取的代码:

@staticmethod
def transform_list_to_dict(para_list):
 """把['a', 'b']转换成{'a':0, 'b':1}的形式
 @param para_list: 列表,里面是每个列对应的字段名
 @return: 字典,里面是字段名和位置的映射
 """
 res_dict = {}
 idx = 0
 while idx < len(para_list):
  res_dict[str(para_list[idx]).strip()] = idx
  idx += 1
 return res_dict
 
@staticmethod
def map_fields_list_schema(fields, list_schema):
 """根据字段的模式,返回模式和数据值的对应值;例如 fields为['a','b','c'],schema为{'name', 'age'},那么就返回{'name':'a','age':'b'}
 @param fields: 包含有数据的数组,一般是通过对一个Line String通过按照\t分割得到
 @param list_schema: 列名称的列表list
 @return: 词典,key是字段名称,value是字段值
 """
 dict_schema = FileUtil.transform_list_to_dict(list_schema)
 return FileUtil.map_fields_dict_schema(fields, dict_schema)

使用的时候,可以用列表的形式配置模式,不需要配置索引更加简洁:

# coding:utf8
"""
@author: www.crazyant.net
测试使用列表模式加载数据列表
优点:如果读取所有列,用列表模式只需要按顺序写出各个列的字段名就可以
缺点:不能够只读取关心的字段,需要全部读取
"""
import file_util
import pprint
 
# 配置好的要读取的列表模式,只能配置前面的列,或者所有咧
list_schema = ["userid", "username", "usertype"]
for fields in file_util.FileUtil.read_file_data("userfile.txt"):
 # 将字段列表,按照字典模式进行映射
 dict_fields = file_util.FileUtil.map_fields_list_schema(fields, list_schema)
 pprint.pprint(dict_fields)

运行结果和字典模式的完全一样。

file_util.py全部代码

以下是file_util.py中的全部代码,可以放在自己的公用类库中使用

# -*- encoding:utf8 -*-
'''
@author: www.crazyant.net
@version: 2014-12-5
'''
 
class FileUtil(object):
 '''文件、路径常用操作方法
 '''
 @staticmethod
 def read_file_data(filepath):
  '''根据路径按行读取文件, 参数filepath:文件的绝对路径
  @param filepath: 读取文件的路径
  @return: 按\t分割后的每行的数据列表
  '''
  fin = open(filepath, 'r')
  for line in fin:
   try:
    line = line[:-1]
    if not line: continue
   except:
    continue
   
   try:
    fields = line.split("\t")
   except:
    continue
   # 抛出当前行的分割列表
   yield fields
  fin.close()
 
 @staticmethod
 def transform_list_to_dict(para_list):
  """把['a', 'b']转换成{'a':0, 'b':1}的形式
  @param para_list: 列表,里面是每个列对应的字段名
  @return: 字典,里面是字段名和位置的映射
  """
  res_dict = {}
  idx = 0
  while idx < len(para_list):
   res_dict[str(para_list[idx]).strip()] = idx
   idx += 1
  return res_dict
 
 @staticmethod
 def map_fields_list_schema(fields, list_schema):
  """根据字段的模式,返回模式和数据值的对应值;例如 fields为['a','b','c'],schema为{'name', 'age'},那么就返回{'name':'a','age':'b'}
  @param fields: 包含有数据的数组,一般是通过对一个Line String通过按照\t分割得到
  @param list_schema: 列名称的列表list
  @return: 词典,key是字段名称,value是字段值
  """
  dict_schema = FileUtil.transform_list_to_dict(list_schema)
  return FileUtil.map_fields_dict_schema(fields, dict_schema)
 
@staticmethod
def map_fields_dict_schema(fields, dict_schema):
 """根据字段的模式,返回模式和数据值的对应值;例如 fields为['a','b','c'],schema为{'name':0, 'age':1},那么就返回{'name':'a','age':'b'}
 @param fields: 包含有数据的数组,一般是通过对一个Line String通过按照\t分割得到
 @param dict_schema: 一个词典,key是字段名称,value是字段的位置;
 @return: 词典,key是字段名称,value是字段值
 """
 pdict = {}
 for fstr, findex in dict_schema.iteritems():
  pdict[fstr] = str(fields[int(findex)])
 return pdict

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用python能有一定的帮助,如果有疑问大家可以留言交流。

Python 相关文章推荐
python结合API实现即时天气信息
Jan 19 Python
Python实现的读写json文件功能示例
Jun 05 Python
python库lxml在linux和WIN系统下的安装
Jun 24 Python
对python pandas 画移动平均线的方法详解
Nov 28 Python
Python爬虫文件下载图文教程
Dec 23 Python
Mac安装python3的方法步骤
Aug 09 Python
Python遍历字典方式就实例详解
Dec 28 Python
python logging 重复写日志问题解决办法详解
Aug 04 Python
手把手教你如何用Pycharm2020.1.1配置远程连接的详细步骤
Aug 07 Python
python 解决pycharm运行py文件只有unittest选项的问题
Sep 01 Python
python 发送邮件的示例代码(Python2/3都可以直接使用)
Dec 03 Python
68行Python代码实现带难度升级的贪吃蛇
Jan 18 Python
python导入时小括号大作用
Jan 10 #Python
python实现应用程序在右键菜单中添加打开方式功能
Jan 09 #Python
ubuntu系统下 python链接mysql数据库的方法
Jan 09 #Python
利用Python中unittest实现简单的单元测试实例详解
Jan 09 #Python
Python中的sort()方法使用基础教程
Jan 08 #Python
Python守护进程和脚本单例运行详解
Jan 06 #Python
Python 基础知识之字符串处理
Jan 06 #Python
You might like
php实现两表合并成新表并且有序排列的方法
2014/12/05 PHP
php实现文本数据导入SQL SERVER
2015/05/17 PHP
PHP基于双向链表与排序操作实现的会员排名功能示例
2017/12/26 PHP
jquery选择器的选择使用及性能介绍
2013/01/16 Javascript
简单的邮箱登陆的提示效果类似于yahoo邮箱
2014/02/26 Javascript
javascript模拟php函数in_array
2015/04/27 Javascript
async/await与promise(nodejs中的异步操作问题)
2017/03/03 NodeJs
Angular中响应式表单的三种更新值方法详析
2017/08/22 Javascript
vue项目中在外部js文件中直接调用vue实例的方法比如说this
2019/04/28 Javascript
让 babel webpack vue 配置文件支持智能提示的方法
2019/06/22 Javascript
vscode vue 文件模板的配置方法
2019/07/23 Javascript
JS数组方法reduce的用法实例分析
2020/03/03 Javascript
如何在微信小程序中使用骨架屏的步骤
2020/06/12 Javascript
SpringBoot+Vue 前后端合并部署的配置方法
2020/12/30 Vue.js
解读Python中degrees()方法的使用
2015/05/18 Python
Python中的推导式使用详解
2015/06/03 Python
Python之多线程爬虫抓取网页图片的示例代码
2018/01/10 Python
Python实现GUI学生信息管理系统
2020/04/05 Python
python用户管理系统
2018/03/13 Python
python 读取DICOM头文件的实例
2018/05/07 Python
PyCharm设置SSH远程调试的方法
2018/07/17 Python
pandas的唯一值、值计数以及成员资格的示例
2018/07/25 Python
Python环境管理virtualenv&amp;virtualenvwrapper的配置详解
2020/07/01 Python
Python绘制数码晶体管日期
2021/02/19 Python
打造经典复古风格的品牌:Alice + Olivia(爱丽丝+奥利维亚)
2016/09/07 全球购物
英国简约舒适女装品牌:Great Plains
2018/07/27 全球购物
英国伦敦的睡衣品牌:Asceno
2019/10/06 全球购物
毕业生自荐书
2013/12/18 职场文书
有创意的广告词
2014/03/18 职场文书
2014个人四风对照检查材料思想汇报
2014/09/18 职场文书
基层党员群众路线教育实践活动个人对照检查材料思想汇报
2014/10/05 职场文书
党员民主评议个人总结
2014/10/20 职场文书
离婚案件上诉状
2015/05/23 职场文书
电影复兴之路观后感
2015/06/02 职场文书
机械原理课程设计心得体会
2016/01/15 职场文书
PHP中国际化的字符串排序和比较对象详解
2021/08/23 PHP