Python实现的将文件每一列写入列表功能示例【测试可用】

栏目: 编程语言 · Python · 发布时间: 8年前

内容简介:这篇文章主要介绍了Python实现的将文件每一列写入列表功能,涉及Python文件读取、遍历、序列追加、赋值等相关操作技巧,需要的朋友可以参考下

本文实例讲述了 Python 实现的将文件每一列写入列表功能。分享给大家供大家参考,具体如下:

# -*- coding: utf-8 -*-
#! python3
'''
python读取文件,每一列写入一个列表
'''
def readFile(cor):
  data = []
  with open(cor,encoding='utf-8') as fr:
    lines = fr.readlines()
  sent_, pin_, tag_ = [], [], []
  for line in lines:
#    print("读取的数据为:%s"%line)
    if line != '\n':
      [char, yin, label] = line.strip().split()
      sent_.append(char)
      pin_.append(yin)
      tag_.append(label)
  data.append((sent_, pin_, tag_))
  sent_, pin_, tag_ = [], [], []
  print(data)
  return data
readFile('fileTmp.txt')

其中fileTmp.txt文件内容如下:

IT pro www.jb51.net

search info www.baidu.com
web news www.sina.com.cn
product buy www.taobao.com

运行结果:

Python实现的将文件每一列写入列表功能示例【测试可用】

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python函数使用技巧总结》、《Python数据结构与算法教程》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Database Design and Implementation

Database Design and Implementation

Edward Sciore / Wiley / 2008-10-24 / 1261.00 元

* Covering the traditional database system concepts from a systems perspective, this book addresses the functionality that database systems provide as well as what algorithms and design decisions will......一起来看看 《Database Design and Implementation》 这本书的介绍吧!

html转js在线工具
html转js在线工具

html转js在线工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具