求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


你至少应该贴几行你文件的内容吧?
时间什么格式啊?

  • 姹俻ython鑴氭湰,浠巘xt妫绱㈠嚭鐗瑰畾瀛楃鐨勮(鏈夊緢澶氳,琛岄噷闈㈡湁璁板綍鐨勬椂闂...
    绛旓細閫愯鍖归厤銆傚浜庢瘡琛屽彲浠ラ鍏堜娇鐢╢ind鏉ョ‘瀹氳琛屼腑鏈夋病鏈夌壒瀹氬瓧绗︺傚鏋滄湁锛屽垯鏍规嵁姝e垯琛ㄨ揪寮忎粠涓彁鍙栨椂闂村瓧绗︺備互涓嬩妇涓涓緥瀛愶紝鍋囪鐗瑰畾瀛楃涓蹭负name锛屾椂闂存牸寮忎负xxxx-xx-xx銆俤ef main(): import re time_format = "\d+-\d+-\d+" #鏃堕棿鏍煎紡 special_string = "name" #鐗瑰畾瀛楃涓 ...
  • 鐢python鍐欎竴涓鑴氭湰鏌ヨtxt鏂囦欢涓煇鍑犱釜鍗曡瘝鍑虹幇鐨勬鏁,骞舵妸缁撴灉杈撳嚭...
    绛旓細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...
  • 璇烽棶澶х浠python濡備綍瀹炵幇,妫鏌201812.txt閲岄潰鐨刟aaa杩欎釜鐢ㄦ埛鍚嶅拰yuyu杩...
    绛旓細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垯杩欓噷鏍规嵁瀹為檯鐨勫尮閰嶅啓 ...
  • 濡備綍鐢Python杞绘澗鍒嗗壊txt澶у瀷鏂囨湰鏂囦欢:涓涓湁鏁堢殑鏂囦欢澶勭悊鑴氭湰
    绛旓細鍑芥暟璇﹁В: split_txt_file(file_path, max_chars, encoding='utf-8')锛岃繖涓嚱鏁扮殑鏍稿績鍦ㄤ簬鍏朵笁涓弬鏁帮細鏂囦欢璺緞銆佹瘡涓垎鍓叉枃浠剁殑鏈澶у瓧绗︽暟锛屼互鍙婇粯璁ょ紪鐮侊紙UTF-8锛夛紝涓鍒囬兘涓轰簡浣犵殑鏂囦欢绠$悊闇姹傞噺韬畾鍒躲傛鏌ヤ笌鍑嗗: 鍑芥暟棣栧厛纭鏂囦欢鏄惁瀛樺湪锛岀‘淇濇垜浠殑璧风偣鍑嗙‘鏃犺銆傚鏋滄枃浠惰矾寰勬寚鍚戠殑鏂囦欢涓嶅瓨鍦...
  • 濡備綍鍒╃敤pyrhon鑴氭湰杈撳嚭txt鏂囦欢鍜宑sv鏂囦欢鐨勭浉鍚屾暟鎹,骞跺皢鍏惰緭鍑轰负txt鏂...
    绛旓細csv_reader:txt_output.write('\t'.join(row) + '\n')```浠ヤ笂浠g爜灏嗘暟鎹繚瀛樹负鍚嶄负 `data.txt` 鐨 txt 鏂囦欢鍜屽悕涓 `data.csv` 鐨 csv 鏂囦欢銆傜劧鍚庯紝瀹冨皢浠 csv 鏂囦欢涓鍙栧唴瀹癸紝骞跺皢鐩稿悓鐨勬暟鎹繚瀛樺埌鍚嶄负 `data_output.txt` 鐨 txt 鏂囦欢涓備綘鍙互鏍规嵁鑷繁鐨勯渶姹備慨鏀规枃浠跺悕鍜岃矾寰勩
  • 鐢python璇诲彇鏂囨湰鏂囦欢,瀵硅鍑虹殑姣忎竴琛岃繘琛屾搷浣,杩欎釜鎬庝箞鍐?
    绛旓細鐢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()...
  • 鍒╃敤Python缂栧啓鍒ゆ柇鏂囦欢鏄惁鏈夐噸澶嶈鐨鑴氭湰
    绛旓細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'))
  • 涓涓txt鏂囨。,宸茬粡鐢ㄧ粨宸村垎璇嶅垎瀹岃瘝,鎬庝箞鐢python宸ュ叿瀵硅繖涓垎瀹岃瘝鐨勬枃 ...
    绛旓細/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...
  • python鎬庝箞鎸txt涓殑鍏抽敭璇嶈幏鍙杄xcel琛屽唴瀹?
    绛旓細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鏄鍒扮殑...
  • python澶勭悊txt鏂囨湰,鎻愬彇鍏朵腑鐨凞N鍊笺丱PTRCL鍊笺丏IV鍊笺 LNATT鍊艰窡COS...
    绛旓細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 ...
  • 扩展阅读:免费外挂辅助 ... python制作app脚本 ... 脚本制作一键生成器 ... python内运行python脚本 ... python游戏挂机脚本 ... python如何编写挂机脚本 ... 万能脚本辅助器 ... python脚本怎么执行 ... python脚本控制手机app ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网