如何在多文字中提取指定資料,並按時間命名檔名

2021-08-09 08:27:48 字數 2471 閱讀 1350

#

」』(三個單引號)

本試驗專案的目的為在e:\demo\res目錄下進行對資料的額遍歷搜尋與x中的相同的關鍵內容,

並且通過提取的關鍵內容儲存在e:\demo\res,並根據『實時日期.txt』的格式進行檔案命名,本次試驗的檔案為在res資料夾中的檔案

被搜尋文字txt為自己預先在e:\demo\res中先設定好,txt文字名字不限

例如以下范文1:

1 2

3 4

5 6

7 gpgpu_n_mem_read_global

8 9

000

gpgpu_n_mem_write_global

11 gpgpu_n_mem_texture

12233

gpgpu_n_mem_const

1232223

12312

3123123

123123

gpgpu_n_param_mem_insn

范文2:

hellow ayu, i miss you.

gpu_sim_insn

gpu_ipc

l1i_total_cache_accesses

l1d_total_cache_accesses

gpgpu_n_tot_thrd_icount

gpgpu_n_tot_w_icount

gpgpu_n_mem_read_local

f 33

4 5

6 2345

34 345

6 345

gpgpu_n_param_mem_insn

」』(三個單引號)

import datetime

import re

import sys

import os,glob

now = datetime.datetime.now() # ->這是時間陣列格式

otherstyletime = now.strftime(「%y_%m_%d %h_%m_%s」)

path = 『e:\demo\res』

fout = open(「e:\demo\res\%s.txt」%otherstyletime, 『w』)

x = [

『gpu_sim_insn』,

『gpu_ipc』,

『l1i_total_cache_accesses』,

『l1d_total_cache_accesses』,

『gpgpu_n_tot_thrd_icount』,

『gpgpu_n_tot_w_icount』,

『gpgpu_n_mem_read_local』,

『gpgpu_n_mem_write_local』,

『gpgpu_n_mem_read_global』,

『gpgpu_n_mem_write_global』,

『gpgpu_n_mem_texture』,

『gpgpu_n_mem_const』,

『gpgpu_n_load_insn』,

『gpgpu_n_store_insn』,

『gpgpu_n_shmem_insn』,

『gpgpu_n_tex_insn』,

『gpgpu_n_const_mem_insn』,

『gpgpu_n_param_mem_insn』

]os.chdir(path)

for filename in os.listdir():

fs = open(filename,』r』,encoding= 『ansi』) #py3必須指定編碼格式 現目前能使用ansi(預設用的標準格式),unicode,unicode big endian,utf-8編碼

#處理檔案中的每一行資料

for line in fs.readlines():

a = line.split()

if a != and a[0] in x:

fout.write(a[0]+』\n』) #原來的指令為fout.write(a[-1]+』\n』),為測試與a[0]的不同所以在以下部分做出改變

if a[0]==a[-1]: #對a[0]&a[-1]進行比較,若兩者相同列印 print(『a[0]==a[-1]』);兩者個不同則列印出print(『a[0]!=a[-1]』)

print(『a[0]==a[-1]』)

else:

print(『a[0]!=a[-1]』)

if a[0] == 『gpgpu_n_param_mem_insn』: #感覺對於此處略微欠缺實際考慮,僅能用於根據』gpgpu_n_param_mem_insn』結尾的文段,仍需要考慮改進

fout.write(『\n』)

break

fout.write(『\n』)

fout.close()

#

如何從檔案中提取指定位置的資料????

如何從檔案中提取指定位置的資料?vcl元件開發及應用 資料如下 begin of epoch gps utc 172800.0 2006 12 12 0 0 0.0 plh ddmmss.ss clk ztd m 385508.36127 770358.37552 40.803 191.177 0....

如何在Excel中提取小數點後面的數字?

excel中,如果某個單元格中包含乙個帶小數,要用公式提取該數值小數點後面的數字,例如a1單元格中包含乙個數值 59178.68 在b1單元格中輸入下面的公式 right a1,len a1 find a1 公式返回結果 68 要取得純小數,還可用mod函式 mod abs a1 1 對於 5917...

c 之從標準流中提取文字資料

istream istream get char int char n istream istream getline char int char n 作用 從文字中提取指定個數的字元,並在串陣列末新增乙個空字元 其中,第乙個引數指向接受字元資料的字元陣列 第二個引數指定字元陣列最多可容納的字元個數...