首页 > 解决方案 > 如何交换字符串并保存

问题描述

我在保存我修改过的文件时遇到问题,我需要在名为DTC_5814_removingswitch_data的原始文件字符串中替换并将其另存为单独的文件我该怎么做,所以这就是程序的基本功能,它打开 eeprom 文件,然后搜索对于两个字符串之间的字符串并将其分组,然后计算数据并通过给定的数据搜索两个字符串之间的其他字符串并修改数据,基本上代码有效我有一个问题如何将其保存为单独的文件,文件保存功能目前没有功能

这是代码:

import re

#checking the structures counting
file = open ("eeprom", "rb") .read().hex()
filesave = open("eepromMOD", "wb")

DTC_data = re.search("ffff30(.*)100077", file)
DTC_data_final = print (DTC_data.group(1))

#finds string between two strings in 2nd line of eeprom file

switch_data = re.search("010607(.*)313132", file)
switch_data_final = print (switch_data.group(1))

#finds string betwenn two strings in 3rd line of eeprom file


DTC_data_lenght = (len(DTC_data.group(1)))
#lenght of the whole DTC_data group

DTC_312D = re.search("ffff30(.*)312d", file)
DTC_3036 = re.search("ffff30(.*)3036", file)
DTC_5814 = re.search("ffff30(.*)5814", file)
#searching for DTC 312D

DTC_312D_lenght = (len(DTC_312D.group(1))+4)
DTC_312D_lenght_start =(len(DTC_312D.group(1)))
DTC_3036_lenght = (len(DTC_3036.group(1))+4)
DTC_3036_lenght_start =(len(DTC_3036.group(1)))
DTC_5814_lenght = (len(DTC_5814.group(1))+4)
DTC_5814_lenght_start =(len(DTC_5814.group(1)))
#confirming the lenght of the DTC table

if DTC_312D_lenght <= DTC_data_lenght and DTC_312D_lenght%4==0 :
    #If dtc lenght shorter than whole table and devidable by 4
    print("Starting DTC removal")
    #Printing for good count
    switch_data_lenght = (len(switch_data.group(1)))
    #Counting switch data table
    DTC_312D_removing = switch_data.group(1)[:DTC_312D_lenght_start] + "0000" + switch_data.group(1)[DTC_312D_lenght:]
    #Read from data group (data[:define start] + "mod to wish value" + data[define end]
    print(DTC_312D_removing)
else:
    print("DTC non existant or incorrect")

if DTC_3036_lenght <= DTC_data_lenght and DTC_3036_lenght%4==0 :
    #If dtc lenght shorter than whole table and devidable by 4
    print("Starting DTC removal")
    #Printing for good count
    switch_data_lenght = (len(switch_data.group(1)))
    #Counting switch data table
    DTC_3036_removing = DTC_312D_removing[:DTC_3036_lenght_start] + "0000" + switch_data.group(1)[DTC_3036_lenght:]
    #Read from data group (data[:define start] + "mod to wish value" + data[define end]
    print(DTC_3036_removing)
else:
    print("DTC non existant or incorrect")

if DTC_5814_lenght <= DTC_data_lenght and DTC_5814_lenght%4==0 :
    #If dtc lenght shorter than whole table and devidable by 4
    print("Starting DTC removal")
    #Printing for good count
    switch_data_lenght = (len(switch_data.group(1)))
    #Counting switch data table
    DTC_5814_removing = DTC_3036_removing[:DTC_5814_lenght_start] + "0000" + switch_data.group(1)[DTC_5814_lenght:]
    #Read from data group (data[:define start] + "mod to wish value" + data[define end]
    print(DTC_5814_removing)
else:
    print("DTC non existant or incorrect")

标签: python-3.x

解决方案


解决了

File_W = file.replace(switch_data.group(1), DTC_5814_removing)
File_WH = binascii.unhexlify(File_W)
filesave.write(File_WH)
filesave.close()

推荐阅读