Python批量提取txt文件中的特定字符后的数字? python:提取txt文件中特定内容后的数据到excel
Python\u5982\u4f55\u6279\u91cf\u63d0\u53d6txt\u6587\u4ef6\u4e2d\u7684\u6307\u5b9a\u4f4d\u7f6e\u7684\u6570\u5b57a = 3b = 5with open('foobar.txt', 'r') as fin: for i, line in enumerate(fin): if i == a: print(line[b]) break
\u9996\u5148\uff0c\u4f60\u7684TXT\u6587\u4ef6\u683c\u5f0f\u662f\u5426\u6e05\u6670\uff0c\u5982\u679c\u662f\u7c7b\u4f3c\u8868\u683c\u7684\u8bdd\uff0c\u5047\u5982IPC\u5728\u6bcf\u884c\u7b2c\u4e09\u4e2a\u4f4d\u7f6e\uff0c\u4f60\u53ef\u4ee5\u6309\u884c\u8bfb\u53d6\uff0c\u7136\u540esplit\u526a\u5207\uff0c\u5f97\u5230IPC\u6570\u636e
\u6700\u597d\u628aTXT\u8d34\u4e0a\u6765\u6bd4\u8f83\u597d
2、待读取文件
是以":"作为分隔符的数据,每一行以回车结束。此文件为XXX.train
3、读取每一句中的汉字
1234567891011...file_train = os.path.join(rootDir,"data/train/rg_train_"+modle_date+"_"+aiscene+".train")with open(file_train, 'r')as fp: textlist = fp.readlines() for text in textlist: if ":" in text: L4ID = text.split(":")[-2] Msg = text.split(":")[-1] if query_start == Msg.strip("
"): print("Msg is in train:",Msg)...
代码中先获取文件,然后读取每一行,然后以":"作为分隔符。(-1代表倒数第一个,-2代表倒数第二个)
不管是txt文件还是xml文件还是其他的,都可以用这种方法来批量替换文件中字符串:
1234567891011121314151617# -*- coding:utf-8 -*-__author__ = 'ShawDa' import glob xmls = glob.glob('xml_files/*.xml')for one_xml in xmls: print(one_xml) f = open(one_xml, 'r+', encoding='utf-8') all_the_lines = f.readlines() f.seek(0) f.truncate() for line in all_the_lines: line = line.replace('dog', 'pig') line = line.replace('cat', 'bike') f.write(line) f.close()
第16行,'a'应该改成a
绛旓細Python 璇诲啓鏂囨湰鏂囦欢棣栧厛闇瑕佹敞鎰忕殑鏄紝txt鏂囦欢鏄叿鏈夊瓧绗︾紪鐮佺殑锛屼笉鍚岀殑txt瀛楃缂栫爜鍙兘涓嶅悓銆傚叿浣撴槸浠涔堢紪鐮侊紝鍙互鐢 notepad++ 绛夋枃鏈紪杈戝櫒鏌ョ湅銆傝鍙栨枃浠跺缓璁娇鐢 with...as... 缁撴瀯锛屽彲浠ヨ嚜鍔ㄥ叧闂枃浠躲倃ith open("text.txt", "r") as f: text = f.read()print(text)濡傛灉涓嶇敤 with.....
绛旓細1.灏嗚繖浜TXT鏂囦欢鍏堝悎骞 2.鎵惧埌杩欎簺鍏蜂綋鏁板肩殑鐗瑰緛 3.鐢ㄦ鍒欒〃杈惧紡杩囨护鎻愬彇 杩欓樀瀛愭瀛︿範python,涓嶄粙鎰忓彲浠ュ彂缁欐垜锛岀粌鐫鐜
绛旓細def iterdatainfile(filename, spliter='\t'): with open(filename, 'rt') as handle: for ln in handle: yield ln.split(spliter) focue, LF = 1, '\n'with open("output.txt", 'wt') as handle: handle.writelines([row[focue] + LF for row in iterdatainfile(...
绛旓細瀵逛簬鍒嗘瀽鑰岃█锛屽浘鐗囧拰鍥捐〃鐨勯渶姹傜浉瀵硅緝灏戯紝txt鏍煎紡宸茬粡鑳芥弧瓒冲熀鏈殑鏂囨湰鎻愬彇闇姹傘傚綋鐒讹紝濡傛灉闇瑕佽繘涓姝ュ鐞嗭紝姣斿鏁寸悊鎴怌SV鎴朩ord鏂囨。锛Python搴撹繕鏈夊叾浠栬В鍐虫柟妗堛備緥濡傦紝瀵逛簬闇瑕佹彁鍙栧浘鍍忎腑鐨勬枃瀛楋紝鍙互鍒╃敤鍥惧儚璇嗗埆搴擄紝灏哖DF涓殑鍥剧墖鍐呭杞崲涓烘枃鏈鍑猴紝浠ラ傚簲涓嶅悓鐨勫伐浣滄祦绋嬮渶姹傘
绛旓細def file_pro(file_dir): import os """ 杈撳叆涓涓鏂囦欢澶锛屾枃浠跺す涓嬫槸鎵鏈夊緟澶勭悊鏂囦欢 :param file_dir: 寰呭鐞嗙殑鏂囦欢鎵鍦ㄧ殑鏂囦欢澶 :return: null """ # 鑾峰彇鏂囦欢鍒楄〃 file_list = os.listdir(file_dir) # 寤虹珛缁撴灉鏂囦欢锛屼笌绋嬪簭鍦ㄥ悓绾х洰褰 result_file = ...
绛旓細鍙傝冧竴涓嬶細http://stackoverflow.com/questions/1597649/replace-strings-in-files-by-python#
绛旓細棣栧厛锛屼綘鐨TXT鏂囦欢鏍煎紡鏄惁娓呮櫚锛屽鏋滄槸绫讳技琛ㄦ牸鐨勮瘽锛屽亣濡侷PC鍦ㄦ瘡琛岀涓変釜浣嶇疆锛屼綘鍙互鎸夎璇诲彇锛岀劧鍚巗plit鍓垏锛屽緱鍒癐PC鏁版嵁 鏈濂芥妸TXT璐翠笂鏉ユ瘮杈冨ソ
绛旓細def evid(a,b): import re in_put=open('33.txt') out_put=open('22.txt','w') line=in_put.readlines() temp=[] dic=[] for i in line: print i c= re.search(r'contact_info=(\d+)',i) if c: temp.append(c.group(1)) for i i...
绛旓細杩欎簺閮芥槸鍩烘湰鐨勬搷浣滐紝濡傛病鏈夊叿浣撶殑鏁版嵁婧愬拰鐩爣琛紝娌″姙娉曞府浣犮傜綉涓婂緢瀹规槗鎵惧埌鐩稿叧鐨勪唬鐮侊紝鎿嶄綔txt鏂囦欢銆傚畨瑁卐xcel鎻掍欢鍚庯紝鍒╃敤鎻掍欢璇诲啓excel鍐欏叆鏁版嵁銆
绛旓細fpa=open(mxdPath)fp-1=open("1.txt","w")fp-2=open("2.txt","w")for linea in fpa.readlines(): linea=linea.replace("\n","").split("\t") fp-1.writeline(linea[0]) fp-2.writeline(linea[1])fpa.close()fp-1.close()fp-2.close()棣栧厛鎵撳紑鏁版嵁瀛樻斁鐨鏂囦欢杩涜锛...