求python脚本,从txt检索出特定字符的行(有很多行,行里面有记录的时间),并从行中抓出对应的时间字符 Python如何从一个txt文件中挑出所有带有‘asd’的行...
python \u95ee\u9898 \u6709\u4eba\u77e5\u9053\u600e\u4e48\u8f93\u51fa\u6587\u4ef6\u4e2d\u6bcf\u884c\u9996\u5b57\u7b26\u548c\u5c3e\u5b57\u7b26\u4e92\u6362\u7684\u884c\u201d \u5417\uff1f# -*- coding: cp936 -*-
f=open("test.txt")
for line in f:
norn=line.rstrip()#\u53bb\u9664\u884c\u5c3e\u7a7a\u767d\u5b57\u7b26\u3002
newline=norn[-1]+norn[1:-1]+norn[0]#\u4ea4\u6362\u9996\u5c3e\u5b57\u7b26
print newline
\u5c11\u5973\u5904\u5973\u6740\u624b\u7684\u56de\u7b54\uff0c\u6709\u591a\u5904\u9519\u8bef\uff1a
1. file\u662f\u5185\u7f6e\u51fd\u7d22\u548copen\u529f\u80fd\u4e00\u6837\u3002
2.\u5e94\u8be5\u7ed9r\u52a0\u4e0a\u5f15\u53f7
3.\u5b57\u7b26\u662f\u4e0d\u53ef\u53d8\uff0c\u4e0d\u53ef\u4ee5\u539f\u5730\u4fee\u6539,list[0]="xxx"\u8fd9\u6837\u662f\u4e0d\u884c\u7684\u3002
# -*- coding: utf-8 -*-""":created on: 2015\u5e747\u670813\u65e5:copyright: Nokia Solutions and Networks:author: Chuanqing Qin:contact: [email protected]"""import reimport osif __name__ == '__main__': counter = 0 with open('test.txt', 'r') as f: for line in f: if re.findall(r'.*asd.*', line): counter += 1 print counter
逐行匹配。对于每行可以首先使用find来确定该行中有没有特定字符。如果有,则根据正则表达式从中提取时间字符。
以下举一个例子,假设特定字符串为name,时间格式为xxxx-xx-xx。
def main():
import re
time_format = "\d+-\d+-\d+" #时间格式
special_string = "name" #特定字符串
pattern = re.compile(time_format)
txt_content = open("test.txt", "r")
for line in txt_content:
l = line.strip()
if l.find(special_string)>=0: #如果有特定字符串
print l #打印对应的行
match =pattern.match(l) #如果有匹配的时间格式
if match:
print match.group() #打印对应的时间
if __name__ == '__main__':
main()
样例test.txt为
2014-2-11 Your behavior is causing our name to be dragged through the mud.
2014-2-12 I am so happy to get munificent birthday presents from my friends.
2014-2-13 He's the boss in name only, because I issue all the orders.
2014-2-14 We are happy to give the product our full endorsement.
2014-2-15 His name leaped out at me from the book.
2014-2-16 We'll be happy to help if you need us.
输出结果为
2014-2-11 Your behavior is causing our name to be dragged through the mud.
2014-2-11
2014-2-13 He's the boss in name only, because I issue all the orders.
2014-2-13
2014-2-15 His name leaped out at me from the book.
2014-2-15
你至少应该贴几行你文件的内容吧?
时间什么格式啊?
绛旓細閫愯鍖归厤銆傚浜庢瘡琛屽彲浠ラ鍏堜娇鐢╢ind鏉ョ‘瀹氳琛屼腑鏈夋病鏈夌壒瀹氬瓧绗︺傚鏋滄湁锛屽垯鏍规嵁姝e垯琛ㄨ揪寮忎粠涓彁鍙栨椂闂村瓧绗︺備互涓嬩妇涓涓緥瀛愶紝鍋囪鐗瑰畾瀛楃涓蹭负name锛屾椂闂存牸寮忎负xxxx-xx-xx銆俤ef main(): import re time_format = "\d+-\d+-\d+" #鏃堕棿鏍煎紡 special_string = "name" #鐗瑰畾瀛楃涓 ...
绛旓細bin/python#-*- encoding: utf-8 -*-def counter(path, find, punctuation): infile = open(path, "r") lenth = len(find) count = [] for i in range(lenth): count.append(0) dat = infile.readline().strip("\n") while dat != '': dat = dat.spli...
绛旓細import re#璇诲彇2018鐨勬枃鏈紝鎶婃瘡琛岀殑璁板綍锛屽瓨鍏ユ暟缁剋ith open('201812.txt') as f: test=f.readlines()#鎶妔chema鐨勬枃鏈鍑烘潵with open('schema.txt') as f: a=f.read() #閬嶅巻test鏁扮粍锛屽湪鏂囨湰a涓壘鏄惁鏈夊尮閰嶇殑锛屾湁鍖归厤鐨勫氨鎵撳嵃鍑烘潵for i in test: #姝e垯杩欓噷鏍规嵁瀹為檯鐨勫尮閰嶅啓 ...
绛旓細鍑芥暟璇﹁В: split_txt_file(file_path, max_chars, encoding='utf-8')锛岃繖涓嚱鏁扮殑鏍稿績鍦ㄤ簬鍏朵笁涓弬鏁帮細鏂囦欢璺緞銆佹瘡涓垎鍓叉枃浠剁殑鏈澶у瓧绗︽暟锛屼互鍙婇粯璁ょ紪鐮侊紙UTF-8锛夛紝涓鍒囬兘涓轰簡浣犵殑鏂囦欢绠$悊闇姹傞噺韬畾鍒躲傛鏌ヤ笌鍑嗗: 鍑芥暟棣栧厛纭鏂囦欢鏄惁瀛樺湪锛岀‘淇濇垜浠殑璧风偣鍑嗙‘鏃犺銆傚鏋滄枃浠惰矾寰勬寚鍚戠殑鏂囦欢涓嶅瓨鍦...
绛旓細csv_reader:txt_output.write('\t'.join(row) + '\n')```浠ヤ笂浠g爜灏嗘暟鎹繚瀛樹负鍚嶄负 `data.txt` 鐨 txt 鏂囦欢鍜屽悕涓 `data.csv` 鐨 csv 鏂囦欢銆傜劧鍚庯紝瀹冨皢浠 csv 鏂囦欢涓鍙栧唴瀹癸紝骞跺皢鐩稿悓鐨勬暟鎹繚瀛樺埌鍚嶄负 `data_output.txt` 鐨 txt 鏂囦欢涓備綘鍙互鏍规嵁鑷繁鐨勯渶姹備慨鏀规枃浠跺悕鍜岃矾寰勩
绛旓細鐢python璇诲彇鏂囨湰鏂囦欢锛屽璇诲嚭鐨勬瘡涓琛岃繘琛屾搷浣滐紝鍐欐硶濡備笅锛歠 = open("test.txt", "r")while True:line = f.readline()if line:pass # do something here line=line.strip()p=line.rfind('.')filename=line[0:p]print "create %s"%line else:break f.close()...
绛旓細coding:UTF-8def rept(file):f=open(file,'r')lines=f.readlines()f.close()lines.sort()oldline='z^z^z^z^z^z^z^z^z'k=1for newline in lines:if (newline==oldline)and(k!=1):return Trueelse:oldline=newlinek+=1return Falseprint(rept('c:/python27/rep.txt'))
绛旓細/usr/bin/env python3#-*- coding:utf-8 -*-import os,random#鍋囪瑕佽鍙栨枃浠跺悕涓篴a锛屼綅浜庡綋鍓嶈矾寰刦ilename='aa.txt'dirname=os.getcwd()f_n=os.path.join(dirname,filename)#娉ㄩ噴鎺夌殑绋嬪簭娈碉紝鐢ㄤ簬娴嬭瘯鑴氭湰锛瀹冪敓鎴20琛屾暟鎹紝姣忚鏈1-20闅忔満涓暟瀛楋紝姣忎釜鏁板瓧闅忔満1-20'''test=''for i in...
绛旓細import re#璇诲叆txt鏂囦欢涓殑鍏抽敭璇峸ith open("a.txt", "r") as f:keywords = f.read()#灏嗗叧閿瘝杞崲鎴愭鍒欏尮閰嶇殑鏍煎紡,涔熷氨鏄妸鍚勪釜鍏抽敭璇嶇敤|鍒嗗壊keywords = keywords.replace("\n", "|")if re.search(keywords, cell_value):# 濡傛灉鍖归厤鍒板叧閿瘝鎵ц鍐欏叆excel鐨勬搷浣,杩欓噷cell_value鏄鍒扮殑...
绛旓細logfile = open("file.txt","r") # file.log 灏辨槸浣犵殑鏃ュ織鍚 outfile = open("out.txt","w") # out.txt 灏辨槸杈撳嚭鏂囦欢 while True:out = logfile.readline()dn = out.find ("DN") #鍦ㄦ瘡琛屼腑鏌ユ壘 DN 瀛楃涓 op = out.find("OPTRCL") #鍦ㄦ瘡琛屼腑鏌ユ壘 OPTRCL 瀛楃涓 div ...