Python如何根據(jù)關(guān)鍵字逐行提取文本內(nèi)容問題
Python根據(jù)關(guān)鍵字逐行提取文本內(nèi)容
在獲取測試的一些數(shù)據(jù)時(shí),需要對(duì)數(shù)據(jù)重新提取保存,因此記錄。
原始文本樣式
2018-09-06 16:42 - INFO - Coordinate: [-285.444793701, 1958.66479492, 175.649078369], End of execution. 2018-09-06 16:43 - INFO - Coordinate: [-301.866485596, 1959.87561035, 175.649200439], End of execution. 2018-09-06 16:44 - INFO - Coordinate: [-318.365051275, 1960.27978516, 175.646804815], End of execution. 2018-09-06 16:45 - INFO - Coordinate: [-334.736816406, 1961.48742676, 176.991455078], End of execution. 2018-09-06 16:46 - INFO - Coordinate: [-356.053833008, 1962.49230957, 176.991180429], End of execution. 2018-09-06 16:47 - INFO - Coordinate: [-370.848907471, 1962.69274902, 176.989471436], End of execution. 2018-09-06 16:48 - INFO - Coordinate: [-388.050415039, 1963.19372559, 178.462493896], End of execution.
提取代碼如下
# key words to extract the coordinate str_start = "[" str_end = "]" f_orig = open('/home/yasin/test_coordinate.txt') # original file f_coord = open('coordinate_save.txt', 'w') # target file used to save line = f_orig.readline() while line: # find index according to the key words index_start = line.find(str_start) index_end = line.find(str_end) text = line[index_start : index_end] if text != '': # If there is more than one [], we can use "Coordinate" and "End" as str_start and str_end f_coord.write(str(line[index_start + 1 : index_end]) + '\n') line = f_orig.readline() f_orig.close() f_coord.close()
提取后保存樣式
-285.444793701, 1958.66479492, 175.64907836 -301.866485596, 1959.87561035, 175.64920043 -318.365051275, 1960.27978516, 175.64680481 -334.736816406, 1961.48742676, 176.99145507 -356.053833008, 1962.49230957, 176.99118042 -370.848907471, 1962.69274902, 176.98947143 -388.050415039, 1963.19372559, 178.46249389
有的同學(xué)問如下問題:
根據(jù)vertex關(guān)鍵字在其后面進(jìn)行換行?
我的方法是對(duì)文本按關(guān)鍵字分割并重寫文件,如果各位有更好的方法,可以一起探討!
f_in = open('test.txt') #源文件 f_out = open('out.txt', 'w') #重寫文件 line = f_in.readlines() #整個(gè)文件讀入 key_word = "hello" line_split = line[0].split(key_word) #按指定單詞分割 for i in range(len(line_split)): if len(line_split[i])>0: f_out.write(key_word+":"+line_split[i].strip(",")+"\n") # 去掉逗號(hào)再加上換行即可
python提取關(guān)鍵字之后的段落
import pandas as pd import re # 讀取原始 Excel 文件 df = pd.read_excel(r"C:\Users\win10\Desktop\1.xlsx") # 定義正則表達(dá)式 pattern = re.compile(r'.*Conclusion.*', re.IGNORECASE) # 遍歷每一行數(shù)據(jù) for index, row in df.iterrows(): if isinstance(row["art_content"], str) and pd.notna(row["art_content"]): # 獲取包含 "Conclusion" 的段落所在行號(hào) conclusion_line_index = -1 for i, line in enumerate(row["art_content"].split("\n")): if pattern.match(line): conclusion_line_index = i break if conclusion_line_index != -1: # 獲取從 "Conclusion" 行開始的所有段落 conclusion_paragraphs = row["art_content"].split("\n")[conclusion_line_index + 1 :] # 將所有段落連接為一個(gè)字符串,并去掉首尾空格 conclusion_section = " ".join(conclusion_paragraphs).strip() # 將結(jié)果賦值給對(duì)應(yīng)的 conclusion 列 df.at[index, "conclusion"] = conclusion_section # 將處理后的數(shù)據(jù)寫入新的 Excel 文件 df.to_excel(r"C:\Users\win10\Desktop\工作簿1.xlsx", index=False)
以上代碼中,我們遍歷每個(gè) "art_content" 列的文本內(nèi)容,用循環(huán)定位到包含 "Conclusion" 字樣的段落所在的行號(hào)(如果沒有找到則保持行號(hào)為 -1)。
如果找到了 "Conclusion" 所在的段落,就將該段落以及其以下的所有段落連接成一個(gè)字符串,并賦值給對(duì)應(yīng)的 "conclusion" 列。
處理完畢后,將整個(gè) DataFrame 存入新的 Excel 文件中。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python 如何利用pandas 和 matplotlib繪制柱狀圖
Python 中的 pandas 和 matplotlib 庫提供了豐富的功能,可以幫助你輕松地繪制各種類型的圖表,本文將介紹如何使用這兩個(gè)庫,繪制一個(gè)店鋪銷售數(shù)量的柱狀圖,并添加各種元素,如數(shù)據(jù)標(biāo)簽、圖例、網(wǎng)格線等,感興趣的朋友一起看看吧2023-10-10Python+pyaudio實(shí)現(xiàn)音頻控制示例詳解
PyAudio?是語音處理的?Python?庫,提供了比較豐富的功能。本文將利用pyaudio控制指定設(shè)備,實(shí)現(xiàn)錄制音頻、采集音頻流、播放音頻,感興趣的可以了解一下2022-07-07python檢測主機(jī)的連通性并記錄到文件的實(shí)例
今天小編就為大家分享一篇python檢測主機(jī)的連通性并記錄到文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06Python使用描述符實(shí)現(xiàn)屬性類型檢查的案例解析
這篇文章主要介紹了Python使用描述符實(shí)現(xiàn)屬性類型檢查,實(shí)例屬性就是在一個(gè)類中將另一個(gè)類的實(shí)例作為該類的一個(gè)數(shù)屬性,本文通過代碼演示給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05python常用的各種排序算法原理與實(shí)現(xiàn)方法小結(jié)
這篇文章主要介紹了python常用的各種排序算法原理與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式總結(jié)分析了冒泡排序、插入排序、選擇排序、快速排序等排序算法的相關(guān)原理與實(shí)現(xiàn)方法,需要的朋友可以參考下2023-04-04