首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何交换字符串并保存

如何交换字符串并保存
EN

Stack Overflow用户
提问于 2018-10-08 19:28:10
回答 1查看 22关注 0票数 0

我在保存文件i modifyed时遇到了问题,我需要在原始文件字符串DTC_5814_removingswitch_data中替换,并将其保存为单独的文件文件保存函数目前没有函数

以下是代码:

代码语言:javascript
复制
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")
EN

回答 1

Stack Overflow用户

发布于 2018-10-08 20:42:08

解出

代码语言:javascript
复制
File_W = file.replace(switch_data.group(1), DTC_5814_removing)
File_WH = binascii.unhexlify(File_W)
filesave.write(File_WH)
filesave.close()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52708881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档