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

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

内容简介:这篇文章主要介绍了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程序设计有所帮助。


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

查看所有标签

猜你喜欢:

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

The Smashing Book

The Smashing Book

Jacob Gube、Dmitry Fadeev、Chris Spooner、Darius A Monsef IV、Alessandro Cattaneo、Steven Snell、David Leggett、Andrew Maier、Kayla Knight、Yves Peters、René Schmidt、Smashing Magazine editorial team、Vitaly Friedman、Sven Lennartz / 2009 / $ 29.90 / € 23.90

The Smashing Book is a printed book about best practices in modern Web design. The book shares technical tips and best practices on coding, usability and optimization and explores how to create succes......一起来看看 《The Smashing Book》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器