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程序设计有所帮助。

您可能感兴趣的文章:

  • C++/Php/Python/Shell 程序按行读取文件或者控制台的实现
  • Python实现读取文件最后n行的方法
  • Python按行读取文件的实现方法【小文件和大文件读取】
  • Python按行读取文件的简单实现方法
  • Python3实现从文件中读取指定行的方法
  • python逐行读取文件内容的三种方法
  • python读文件逐行处理的示例代码分享
  • python追加元素到列表的方法
  • Python文件操作,open读写文件,追加文本内容实例
  • Python中使用第三方库xlutils来追加写入Excel文件示例
  • Python创建文件和追加文件内容实例
(0)

相关推荐

  • Python按行读取文件的简单实现方法

    1:readline() file = open("sample.txt") while 1: line = file.readline() if not line: break pass # do something file.close() 一行一行得从文件读数据,显然比较慢: 不过很省内存: 测试读10M的sample.txt文件,每秒大约读32000行: 2:fileinput import fileinput for line in fileinput.input("

  • python读文件逐行处理的示例代码分享

    复制代码 代码如下: import os ## for os.path.isfile() def dealline(line) :    print(line) ## 针对line我可以做很多事情 def getfilename() :    return input('Please input file name(input exit() for exit):').strip() class more : ## MORE功能    linenum = 0    size = 10    def

  • python逐行读取文件内容的三种方法

    方法一: 复制代码 代码如下: f = open("foo.txt")             # 返回一个文件对象  line = f.readline()             # 调用文件的 readline()方法  while line:      print line,                 # 后面跟 ',' 将忽略换行符      # print(line, end = '') # 在 Python 3中使用      line = f.readline()

  • python追加元素到列表的方法

    本文实例讲述了python追加元素到列表的方法.分享给大家供大家参考.具体实现方法如下: scores = ["1","2","3"] # add a score score = int(raw_input("What score did you get?: ")) scores.append(score) # list high-score table for score in scores: print score 运行结

  • Python按行读取文件的实现方法【小文件和大文件读取】

    本文实例讲述了Python按行读取文件的实现方法.分享给大家供大家参考,具体如下: 小文件: #coding=utf-8 #author: walker #date: 2013-12-30 #function: 按行读取小文件 all_lines = [] try: file = open('txt.txt', 'r') all_lines = file.readlines() except IOError as err: print('File error: ' + str(err)) fin

  • C++/Php/Python/Shell 程序按行读取文件或者控制台的实现

    写程序经常需要用到从文件或者标准输入中按行读取信息,这里汇总一下.方便使用 1. C++ 读取文件 #include<stdio.h> #include<string.h> int main(){ const char* in_file = "input_file_name"; const char* out_file = "output_file_name"; FILE *p_in = fopen(in_file, "r"

  • Python3实现从文件中读取指定行的方法

    本文实例讲述了Python3实现从文件中读取指定行的方法.分享给大家供大家参考.具体实现方法如下: # Python的标准库linecache模块非常适合这个任务 import linecache the_line = linecache.getline('d:/FreakOut.cpp', 222) print (the_line) # linecache读取并缓存文件中所有的文本, # 若文件很大,而只读一行,则效率低下. # 可显示使用循环, 注意enumerate从0开始计数,而line

  • Python创建文件和追加文件内容实例

    一.用Python创建一个新文件,内容是从0到9的整数, 每个数字占一行: 复制代码 代码如下: #python >>>f=open('f.txt','w')    # r只读,w可写,a追加 >>>for i in range(0,10):f.write(str(i)+'\n') .  .  . >>> f.close() 二.文件内容追加,从0到9的10个随机整数: 复制代码 代码如下: #python >>>import ran

  • Python实现读取文件最后n行的方法

    本文实例讲述了Python实现读取文件最后n行的方法.分享给大家供大家参考,具体如下: # -*- coding:utf8-*- import os import time import datetime import math import string def get_last_line(inputfile) : filesize = os.path.getsize(inputfile) blocksize = 1024 dat_file = open(inputfile, 'r') las

  • Python中使用第三方库xlutils来追加写入Excel文件示例

    目前还没有更好的方法来追写Excel,lorinnn在网上搜索到以及之后用到的方法就是使用第三方库xlutils来实现了这个功能,主体思想就是先复制一份Sheet然后再次基础上追加并保存到一份新的Excel文档中去. 使用xlutils 代码实现如下: # -*- coding: utf-8 -*- ''' Created on 2012-12-17 @author: walfred @module: XLRDPkg.write_append @description: ''' import o

  • Python文件操作,open读写文件,追加文本内容实例

    1.open使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('thefile.txt') try: all_the_text = file_object.read( ) finally: file_object.close( ) 注:不能把open语句放在try块里,因为当打开文件出现异常时,文件对象file_object无法执行close()方法. 2.读文件读文本文件input

随机推荐