Python如何根據(jù)關(guān)鍵字逐行提取文本內(nèi)容問題
Python根據(jù)關(guān)鍵字逐行提取文本內(nèi)容
在獲取測(cè)試的一些數(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英文文本分詞(無空格)模塊wordninja的使用實(shí)例
今天小編就為大家分享一篇關(guān)于Python英文文本分詞(無空格)模塊wordninja的使用實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02
Python使用sql語句對(duì)mysql數(shù)據(jù)庫(kù)多條件模糊查詢的思路詳解
這篇文章主要介紹了Python使用sql語句對(duì)mysql數(shù)據(jù)庫(kù)多條件模糊查詢的思路詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
使用python創(chuàng)建股票的時(shí)間序列可視化分析
這篇文章主要為大家詳細(xì)介紹了python創(chuàng)建股票的時(shí)間序列可視化分析,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03

