内容简介:这篇文章主要介绍了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程序设计有所帮助。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 优化ElasticSearch写入效率
- golang 创建,读取,写入文件
- Kafka学习笔记 -- 写入数据
- Elasticsearch 写入原理深入详解
- 高频写入redis场景优化
- Laravel log 无法写入问题
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
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》 这本书的介绍吧!