Python如何從txt文件中提取特定數(shù)據(jù)
本段代碼用于,想要從一段txt文件中只提取目標(biāo)數(shù)據(jù)的情況。
代碼:
def get_data(txt_path: str = '', epoch: int = 100, target: str = '', target_data_len: int = 5):
num_list = [] # 將提取出來的數(shù)據(jù)保存到列表,并在最后返回
data = open(txt_path, encoding="utf-8") # 打開文件
str1 = data.read() # 將文件中讀取到的內(nèi)容轉(zhuǎn)化為字符串
data.close() # 關(guān)閉文件
for i in range(0, epoch):
index = str1.find(target) # 查找字符串str1中str2字符串的位置
num_list.append(float(str1[index+len(target):index+len(target)+target_data_len])) # 將需要的數(shù)據(jù)提取到列表中
str1 = str1.replace(target, 'xxxx', 1) # 替換掉已經(jīng)查閱過的地方,' xxxx '表示替換后的內(nèi)容,1表示在字符串中的替換次數(shù)為1
return num_list函數(shù)參數(shù)解釋:
- txt_path 文件路徑
- epoch 這份文本文件中要提取出的數(shù)據(jù)個數(shù),默認(rèn)100
- target 目標(biāo)數(shù)據(jù)的前綴
- target_data_len 目標(biāo)數(shù)據(jù)的長度,默認(rèn)為5
返回值,列表數(shù)據(jù)
使用舉例:
txt文檔內(nèi)容:
x1:273 test3:477 y4:38489 y1:149 x2:423
x1:274 test3:475 y4:37956 y1:152 x2:422
x1:269 test3:473 y4:38156 y1:152 x2:421
x1:271 test3:471 y4:38156 y1:155 x2:418
x1:272 test3:467 y4:38056 y1:158 x2:416
x1:275 test3:466 y4:37956 y1:161 x2:415
使用:
data_path = "D:/program/test/double_camera_data/x_data.txt" # 提取x1的數(shù)據(jù) list_x1 = get_data(data_path, 6, target="x1:", target_data_len=3) # 提取test3的數(shù)據(jù) list_test3 = get_data(data_path, 6, target="test3:", target_data_len=3) # 提取y4的數(shù)據(jù) list_y4 = get_data(data_path, 6, target="y4:", target_data_len=6) print(list_x1) print(list_test3) print(list_y4)
輸出:
[273.0, 274.0, 269.0, 271.0, 272.0, 275.0]
[477.0, 475.0, 473.0, 471.0, 467.0, 466.0]
[38489.0, 37956.0, 38156.0, 38156.0, 38056.0, 37956.0]
附:Python 從不規(guī)則文本中提取有效信息
背景:從一個混有文字和多個表格的word文檔里,提取表格中有效信息
代碼:
from docx import Document
import numpy as np
import pandas as pd
#讀取文件
doc = Document("文件名.docx")
#讀取表格
tables = doc.tables
#print(len(tables))
rlt = []
flag = 0
for t in tables: #每一個表格
rows = t.rows
for r in rows: #每一行
cols = r.cells
for c in cols: #每一個單元格
if flag != 0:
rlt.append(c.text)
flag = 0
continue
if c.text == "不動產(chǎn)所有權(quán)人" or c.text == "不動產(chǎn)權(quán)屬證明" or c.text == "項(xiàng)目名稱" or c.text == "項(xiàng)目地址":
flag = 1
nums = len(rlt)
rlt = np.array(rlt).reshape((nums//4,4))
#print(rlt)
df = pd.DataFrame(rlt,columns= ["不動產(chǎn)所有權(quán)人" ,"不動產(chǎn)權(quán)屬證明" ,"項(xiàng)目名稱","項(xiàng)目地址"])
#print(df)
df.to_excel('rlt.xlsx')總結(jié)
到此這篇關(guān)于Python如何從txt文件中提取特定數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Python從txt文件提取數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中如何正確使用正則表達(dá)式的詳細(xì)模式(Verbose mode expression)
許多程序設(shè)計(jì)語言都支持利用正則表達(dá)式進(jìn)行字符串操作,python自然也不例外,下面這篇文章主要給大家介紹了關(guān)于在python中如何正確使用正則表達(dá)式的詳細(xì)模式(Verbose mode expression)的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-11-11
Python3中l(wèi)ambda表達(dá)式與函數(shù)式編程講解
今天小編就為大家分享一篇關(guān)于Python3中l(wèi)ambda表達(dá)式與函數(shù)式編程講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
scipy.interpolate插值方法實(shí)例講解
這篇文章主要介紹了scipy.interpolate插值方法介紹,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
Django admin 實(shí)現(xiàn)search_fields精確查詢實(shí)例
這篇文章主要介紹了Django admin 實(shí)現(xiàn)search_fields精確查詢實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
基于Python創(chuàng)建語音識別控制系統(tǒng)
這篇文章主要介紹了通過Python實(shí)現(xiàn)創(chuàng)建語音識別控制系統(tǒng),能利用語音識別識別說出來的文字,根據(jù)文字的內(nèi)容來控制圖形移動,感興趣的同學(xué)可以關(guān)注一下2021-12-12
Windows下實(shí)現(xiàn)Python2和Python3兩個版共存的方法
這篇文章主要介紹了Windows下實(shí)現(xiàn)Python2和Python3兩個版共存的方法,本文詳細(xì)的給出了操作步驟和設(shè)置完成后的使用方法,需要的朋友可以參考下2015-06-06
python關(guān)于調(diào)用函數(shù)外的變量實(shí)例
今天小編就為大家分享一篇python關(guān)于調(diào)用函數(shù)外的變量實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12

