[英]Validating saved text file with Python to check if parameters are set
我在驗證文本文件時遇到問題。 我需要檢查我設置的參數是否正確保存。 文件名是實際日期和時間,我需要檢查發送的參數是否在此文本(日志)文件中。 您可以在下面找到我的代碼:
Arguments 與 argpars 一起發送,例如。 parser.add_argument(“頻率”,類型=int)
print('Saving Measurement...')
print(inst.write(':MMEMory:STORe:TRACe 0, "%s"' % timestr)) #Saving file on the inst
time.sleep(1) #Wait for file to save
print('Downloading file from device...')
ftp = FTP('XX.XX.XXX.XXX')
ftp.login()
ftp.retrbinary('RETR %s'% timestr + '.spa', open(timestr + '.spa', 'wb').write) #downloading saved file into a directory where you run script
print('Done, saved as: ' + timestr)
time.sleep(1)
with open (timestr + '.spa') as f:
if (str(args.freq)) in f.read():
print("saved correctly")
ftp.delete(timestr + '.spa') #Delete file from inst
ftp.quit()
我不確定它是否適合我。 謝謝您的幫助
您可以使用re module
來幫助您在文件中找到日期模式。 我會給你一個小的示例代碼,至少在這種情況下,搜索這個日期模式 dd-mm-yyyy
import re
filepath = 'your-file-path.spa'
regex = '\d\d-\d\d-\d\d\d\d'
with open(filepath, 'r') as f:
file = f.read()
dates_found = re.findall(regex, file)
# dates_found will be an array with all the dates found in the file
print(dates_found)
您可以使用任何您想要的正則表達式作為re.findall(regex, file)
的第一個參數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.