1. 程式人生 > >Python 獲取檔案中最長行的長度和最長行

Python 獲取檔案中最長行的長度和最長行

# Get the number of lonest line
f = open('nihao')
longestLine = max(len(line.strip()) for line in f)
f.close()
print longestLine

# Get the longest line
f = open('nihao')
allLines = [line.strip() for line in f]
f.close()

longest = 0
longLine = ""
for line in allLines:
    linelen = len(line)
    if linelen > longest:
        longest = linelen
        longLine = line
print 'Longest line: %s' % longLine
print 'The number of longest line: %s' % longest
    

相關推薦

Python 獲取檔案長度

# Get the number of lonest line f = open('nihao') longestLine = max(len(line.strip()) for line in f) f.close() print longestLine # Get t

Python 獲取檔案單詞數,字元,字元數

# Get all words list f = open('nihao') wordList = [word for line in f for word in line.split()] # Get number of words wordListLen = len(

python檔案讀取資料,同時去除掉空格

從檔案中讀取資料,同時去除掉空格和換行,程式碼如下 import numpy as np def sort(path): w = open(path,'r') l = w.readlines() col=[] for k in l: k = k.strip('\n')

python判斷檔案有否重複,逐檔案檢測另一檔案是否存在所讀內容

#!/bin/env python # coding:utf-8 #程式功能是為了完成判斷檔案中是否有重複句子 #並將重複句子打印出來 res_list = [] f = open('./downloadmd5.txt','r') res_dup = [] index = 0 file_d

python】啟動一個http服務,用於獲取檔案的資訊

使用場景:想要在瀏覽器下檢視某個機器下的目錄檔案;想使用wget臨時的下載某個機器下的檔案。 首先在目標機器下使用python啟動一個http服務: python -m SimpleHTTPServer     ---------這樣會啟動一個埠為預設埠8000的HTTP

python 獲取檔案

#如果要統計檔案的行數,可以這樣寫: count = len(open(filepath, 'r').readlines()) #這種方法簡單,但是可能比較慢,當檔案比較大時甚至不能工作。 #可以利用enumerate(): count = 0 for index, line in enumerate(op

Python獲取檔案目錄下視訊時、大小、並寫入excle檔案

關鍵詞:os moviepy xlwt # -*- coding=utf-8 -*- import os import sys import xlwt from moviepy.editor import VideoFileClip file_dir = u"G:/視訊

python刪除檔案含"指定內容"的

#!/bin/env python import shutil, sys, os darray = [ "Entering directory", "In function ", "Leaving directory", "__NR_SYSCALL_BASE",

python 去除檔案多個換

轉自:http://yiyunyaya.blog.163.com/blog/static/1654496362013265749168/ 今天在部落格園中看到一篇博文《python 去除多個換行》,試了作者的方法後,與之有很大的差別(不知道是不是由於用的python版本不

python獲取檔案倒數第n

#獲取檔案倒數第n行 def getTail(file,num): readlen=1024 #每次多偏移1024位元組 readoffset=0 #每次從檔案末尾向前偏移量 l

python 獲取字串出現次數多的字母

獲取字串中出現次數最多的字母        給定一個包含不同的英文字母和標點符號的文字,找出其中出現最多的字母,不區分大小寫,並返回一個小寫字母,若存在相同次數的字母,則返回字母表中最先出現的那個。 採用內建函式max import string

[python]獲取網頁內容為漢字的字符串的判斷

vsr rbo ats art htm acad for swe lin IPerf%E2%80%94%E2%80%94%E7%BD%91%E7%BB%9C%E6%B5%8B%E8%AF%95%E5%B7%A5%E5%85%B7%E4%BB%8B%E7%BB%8D%E4%B

python之enumerate函數:獲取列表每個元素的索引

pytho pre highlight blog clas dex color int num 源碼舉例: 1 def enumerate_fn(): 2 ‘‘‘ 3 enumerate函數:獲取每個元素的索引和值 4 :return:打印

python 獲得列表每個元素出現次數的快方法

random ons zip size spa body name import clas import collections import numpy as np import random import time def list_to_dict(lst):

python獲取linuxtop信息

python獲取linux中top信息import os,time,sysimport paramiko,pexpect 獲取日期格式: def get_year_mon_day_hour_min_sec(): time_array = time.localtime() result= "%s

Python 同一檔案,有unittest不執行“if __name__ == '__main__”,不生成HTMLTestRunner測試報告的解決方案

1、問題:Python中同一個.py檔案中同時用unittest框架和HtmlReport框架後,HtmlReport不被執行。 2、為什麼?其實不是HtmlReport不被執行,也不是HtmlReport不生成測試報告,是因為if __name__ == '__main__'中的程式碼根本沒執

Python獲取list指定元素的索引

在平時開發過程中,經常遇到需要在資料中獲取特定的元素的資訊,如到達目的地最近的車站,櫥窗裡面最貴的物品等等。怎麼辦?看下面 方法一: 利用陣列自身的特性 list.index(target), 其中a是你的目標list,target是你需要的下標對應的值 1 li = [10,8,9,26,72,6

python 獲取除內置方法外的所有方法名

self callable pro clas top end method __name__ __main__ #!/usr/bin/env python# !-*- coding:utf-8 -*-class Menu: def __init__(self):

Linux下,grep獲取檔案的最後一次匹配項

如題:使用grep獲取檔案中的最後一次匹配項 grep '查詢的內容' -A 100 檔名 | tail -n 101 grep獲取檔案中的最後一次匹配項,以及後面100行, 注意是tail -n 101而不是tail -n 100,因為tail -n 100將不會顯示匹配內

python獲取序列元素及其出現的次數

有時,我們會在一個包含多個重複元素的序列中,查找出現次數最多的元素。 data = ['a', 'b', 'c', 'a', 't', 'p', 't', 'a', 'b', 'c', 'c', 'a', 't', 'p', 'l', 'm', 'n', 'b', 'd', 'l'