readlines

Python: Undo a file readline() operation so file-pointer is back in original state

被刻印的时光 ゝ 提交于 2019-11-27 11:55:18
I'm browsing through a Python file pointer of a text file in read-only mode using file.readline() looking for a special line. Once I find that line I want to pass the file pointer to a method that is expecting the file pointer to be at the START of that readline (not right after it.) How do I essentially undo one file.readline() operation on a file pointer? You have to remember the position by calling file.tell() before the readline and then calling file.seek() to rewind. Something like: fp = open('myfile') last_pos = fp.tell() line = fp.readline() while line != '': if line == 'SPECIAL': fp

Break string into list of characters in Python

时光毁灭记忆、已成空白 提交于 2019-11-27 01:23:27
So what i want to do is essentially suck a line of txt from a .txt file, then assign the characters to a list, and then creat a list of all the separate characters in a list. So a list of lists. At the moment, I've tried: fO = open(filename, 'rU') fL = fO.readlines() and that's all im up to. I dont quite know how to extract the single characters and assign them to a new list. I want to do something like: fL = 'FHFF HHXH XXXX HFHX' ^^^ so that being the line i got from the .txt file. And then turn it into this: ['F', 'H', 'F', 'F', 'H' ...] ^^^ and that being the new list, with each single

一些需要注意的

倖福魔咒の 提交于 2019-11-26 22:34:13
## r+ 模式: 读,并追加,不管前面读多少字符,都是追加到原文件最后# f = open('rfile',mode='r+',encoding='utf-8')# content = f.read(6)# print(f.tell())# f.write("R+模式")# print(content)# f.close() ## r+ 模式: 读,并追加,不管前面读多少字符,都是追加到原文件最后 # f = open('rfile',mode='r+',encoding='utf-8') # content = f.read(6) # print(f.tell()) # f.write("R+模式") # print(content) # f.close() # truncaate() ,必须有写入权限# with 主动关闭文件甸柄,可多打多个文件# with open('1.txt',encoding='utf-8') as f1, \# open('2.txt',encoding='utf-8') as f2:# for x in f1.readlines(): # 缩进 readlines()是一次性工作,读入内存后迭代完成就没有了# for y in f2.readlines():# print(x.strip() + y.strip())# with open('1

Using “readlines()” twice in a row

点点圈 提交于 2019-11-26 22:12:22
问题 I'm trying to do something like this: Lines = file.readlines() # do something Lines = file.readlines() but the second time Lines is empty. Is that normal? 回答1: Yes, because .readlines() advances the file pointer to the end of the file. Why not just store a copy of the lines in a variable? file_lines = file.readlines() Lines = list(file_lines) # do something that modifies Lines Lines = list(file_lines) It'd be far more efficient than hitting the disk twice. (Note that the list() call is

Python: Undo a file readline() operation so file-pointer is back in original state

流过昼夜 提交于 2019-11-26 15:45:48
问题 I'm browsing through a Python file pointer of a text file in read-only mode using file.readline() looking for a special line. Once I find that line I want to pass the file pointer to a method that is expecting the file pointer to be at the START of that readline (not right after it.) How do I essentially undo one file.readline() operation on a file pointer? 回答1: You have to remember the position by calling file.tell() before the readline and then calling file.seek() to rewind. Something like:

Break string into list of characters in Python

穿精又带淫゛_ 提交于 2019-11-26 09:43:44
问题 Essentially I want to suck a line of text from a file, assign the characters to a list, and create a list of all the separate characters in a list -- a list of lists. At the moment, I\'ve tried this: fO = open(filename, \'rU\') fL = fO.readlines() That\'s all I\'ve got. I don\'t quite know how to extract the single characters and assign them to a new list. The line I get from the file will be something like: fL = \'FHFF HHXH XXXX HFHX\' I want to turn it into this list, with each single

How to read a file without newlines?

久未见 提交于 2019-11-25 22:58:36
问题 In Python, calling temp = open(filename,\'r\').readlines() results in a list in which each element is a line in the file. It\'s a little stupid but still: readlines() also writes newline character to each element, something I do not wish to happen. How can I avoid it? 回答1: You can read the whole file and split lines using str.splitlines: temp = file.read().splitlines() Or you can strip the newline by hand: temp = [line[:-1] for line in file] Note: this last solution only works if the file

python中read,readline,和readlines的区别 并逐行输出

情到浓时终转凉″ 提交于 2019-11-25 22:10:51
python中有神奇的三种读操作:read、readline和readlines read() : 一次性读取整个文件内容。推荐使用read(size)方法,size越大运行时间越长 readline() :每次读取一行内容。内存不够时使用,一般不太用 readlines() :一次性读取整个文件内容,并按行返回到list,方便我们遍历 下面看代码: #!/usr/bin/python # -*- coding: utf-8 -*- import linecache f = open("a.txt") lines = f.read() print lines #for i in lines: # print i # print "===" print (type(lines)) f.close() print 35*"=" fr = open("a.txt") r = fr.readline() print(type(r)) #print r while r: print r, r = fr.readline() print "rrr=",r fr.close() print 35*"=" frs = open("a.txt") rls = frs.readlines() print(type(rls)) print rls for i in rls: print i, frs

How to read a file line-by-line into a list?

若如初见. 提交于 2019-11-25 21:36:14
问题 This post is a Community Wiki . Edit existing answers to improve this post. It is not currently accepting new answers. How do I read every line of a file in Python and store each line as an element in a list? I want to read the file line by line and append each line to the end of the list. 回答1: with open(filename) as f: content = f.readlines() # you may also want to remove whitespace characters like `\n` at the end of each line content = [x.strip() for x in content] 回答2: See Input and Ouput:

一次公司需求记录,python处理sysstat收集的sa性能数据

筅森魡賤 提交于 2019-11-25 20:25:01
鉴于每个月需要把公司上千台的服务的cpu使用利率,CPU负载,内存使用率、网络流量等数据取出,以前是手动通过zabbix的平台去获取,每次都需要花费1-2小时才能完成这个需求,太耗时,因此优化为systat软件采集数据,然后通过python脚本处理/var/log/sa下采集到数据,然后入库到mysql数据库中,以后每次取数据,只需要一行sql代码就能获取到数据,方便快捷,本文把采集数据的脚本截图,如下所示: 最后附上完整代码: #!/bin/python2.7 #coding: utf-8 #################脚本用法:要取哪天的数据就输入哪天的时间,比如9月1输入应为20190901 import os import sys import datetime import commands import time def get_cpu(sdate,edate): cpunum = int(commands.getoutput("cat /proc/cpuinfo |grep processor|wc -l")) path = os.popen("find /var/log/sa/ -type f -newermt "+sdate+" ! -newermt "+edate+" |grep -v sar").readlines() path = [m.rstrip("