【问题标题】:Insert a data on specific position in a text file, Python在文本文件中的特定位置插入数据,Python
【发布时间】:2020-09-07 10:32:15
【问题描述】:

我需要计算新的 CRC 并将更新后的值插入文件中黄色突出显示的位置。

【问题讨论】:

    标签: python text file-handling


    【解决方案1】:

    你可以试试这个:

    import re
    
    regex = r"(?<=CRC_END\n)(.*)(;)"
    
    
    subst = "newvalue "
    
    # You can manually specify the number of replacements by changing the 4th argument
    result = re.sub(regex, subst, YOUR_STRING, 0, re.MULTILINE)
    
    if result:
        print (result)
    

    【讨论】:

      【解决方案2】:

      对于行中的行: 计数=计数+1 如果“CRC16”在行: crcLineNumber = 计数

          Lines[crcLineNumber-1] = inputCrc[:-1].upper()+"h       ;   CRC16 - checksum, MSB - LSB\n"
          file1.close()
      

      【讨论】:

        猜你喜欢
        • 2013-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-30
        • 2021-11-12
        • 1970-01-01
        • 2012-02-20
        相关资源
        最近更新 更多