python读取特定的行

fr = open(filename)
for line in fr.readlines():
    if line.startswith("#"):
        continue
    else:
        ## do something with line
        
## could use fr.readlines()[1:]  from second line

import linecache
content_list = linecache.getlines(filename)[1:]     ## Here using getlines

fourth_line = linecache.getline(filename, 4)        ## Here using getline
## the above method is simpler.
时间: 2024-08-01 22:41:54

python读取特定的行的相关文章

vbs 读取txt是读取特定的行

读取第三行的内容:Set fso = CreateObject("scripting.filesystemobject")path="A.TXT文件的路径"Set A=fso.OpenTextFile(path,1)For i=1 To 2A.ReadLineNextline=A.ReadLineMsgBox "第三行为: "&line 第四行的第2个到第5个字符: Set fso = CreateObject("scripti

python读取excel的行数

基于python3.x下 需要包 from openpyxl import load_workbook 代码如下: 1 from openpyxl import load_workbook 2 wb = load_workbook(filename=r'C:\\Users\\Administrator\\Desktop\\库存大表0803.xlsx') ##读取路径 3 ws = wb.get_sheet_by_name("Sheet1") ##读取名字为Sheet1的sheet表 4

python读取文件指定行

import linecache file=open('3_2.txt','r') linecount=len(file.readlines()) linecache.getline('3_2.txt',linecount) 这样做的过程中发现一个问题,因为我的脚本是循环读取3_2.txt文件,当3_2.txt发生变化时,读到的内容不变化,看了一下linecache应该是缓存的问题,查资料后发现果然是这样,在用linecache之前清除一下缓存就好了linecache.clearcache().

python读取文件指定行内容

import linecache text=linecache.getline(r'C:\Users\Administrator\Desktop\SourceCodeofMongoRedis\chapter_5\generate_string.py',10) 第十行内容为# info = '''1000001 王小小''' 原文地址:https://www.cnblogs.com/tjp40922/p/12393080.html

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

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

Python读取文件行数不对

对于一个大文件,读取每一个行然后处理,用readline()方法老是读不全,会读到一半就结束,也不报错: 总之处理的行数跟 wc -l 统计的不一样,调试了一下午,改用 with open('xxx.log') as fin: for line in fin: do something with line 成功解救,但是不知道是什么原因.网上有说是文件里有特殊字符,需要用rb模式打开,试了也不行.

python 读取文件 并实现文件相关操作最后导出excel

今天做了一个使用python来提取日志内容,并导出excel的小脚本,代码写的不好,新手勿喷,下面进行一下总结. 需求如下: 1.读取文件行数,并作出excel表 2.读取文件中第7个字段,累加求和,求平均数.并找出其中最大的.做excel表. 3.找出文件中特定字符出现次数,统计,做excel表. 4.自动生成excel名称为实时时间. 读取文件行数第一想法就是 wc -l,虽然python中可以调用shell命令,但毕竟不太好,所以这里用python逐行读取文件并进行累加,算出文件行数...

python读取excel文件(xrld模块)

Python读取excel文件 一.python  xlrd模块 安装 mac 下安装python  xlrd模块 http://www.crifan.com/python_read_excel_xls_file_xlrd/comment-page-1/ python setup.py install 在mac 下出现的错误是 http://stackoverflow.com/questions/18199853/error-could-not-create-library-python-2-7

python re删除匹配行

re.compile(strPattern[, flag]): 这个方法是Pattern类的工厂方法,用于将字符串形式的正则表达式编译为Pattern对象. 第二个参数flag是匹配模式,取值可以使用按位或运算符'|'表示同时生效,比如re.I | re.M. 另外,你也可以在regex字符串中指定模式,比如re.compile('pattern', re.I | re.M)与re.compile('(?im)pattern')是等价的. xxx.com文件内容 $TTL 600 ; 1 day