Python 尝试剥离时出错";0x';

Python 尝试剥离时出错";0x';,python,Python,我试图用下面的代码从十六进制值中去掉“0x”,结果出错了,有人能建议如何修复它吗 with open(r'\\Network\files\build_ver.txt','r+') as f: value = int(f.read(), 16) f.seek(0) write_value = hex(value + 1) final_value = format(write_value, 'x') f.write

我试图用下面的代码从十六进制值中去掉“0x”,结果出错了,有人能建议如何修复它吗

   with open(r'\\Network\files\build_ver.txt','r+') as f:
        value = int(f.read(), 16)
        f.seek(0)
        write_value = hex(value + 1)
        final_value = format(write_value, 'x')
        f.write(final_value)
错误:-

Traceback (most recent call last):
  File "build_ver.py", line 5, in <module>
    final_value = format(write_value, 'x')
ValueError: Unknown format code 'x' for object of type 'str'
回溯(最近一次呼叫最后一次):
文件“build_ver.py”,第5行,在
最终值=格式(写入值“x”)
ValueError:类型为“str”的对象的格式代码“x”未知
内置函数返回字符串值:

>>> hex(123)
'0x7b'
>>> type(hex(123))
<class 'str'>
>>>
因此,它不能在这里使用。相反,您可以通过切片来剥离
0x

with open(r'\\Network\files\build_ver.txt','r+') as f:
    value = int(f.read(), 16)
    f.seek(0)
    write_value = hex(value + 1)[2:]
    f.write(write_value)
[2://code>将获取字符串中除前两个字符以外的所有字符。请参见下面的演示:

>>> hex(123)
'0x7b'
>>> hex(123)[2:]
'7b'
>>>
内置函数返回一个字符串值:

>>> hex(123)
'0x7b'
>>> type(hex(123))
<class 'str'>
>>>
因此,它不能在这里使用。相反,您可以通过切片来剥离
0x

with open(r'\\Network\files\build_ver.txt','r+') as f:
    value = int(f.read(), 16)
    f.seek(0)
    write_value = hex(value + 1)[2:]
    f.write(write_value)
[2://code>将获取字符串中除前两个字符以外的所有字符。请参见下面的演示:

>>> hex(123)
'0x7b'
>>> hex(123)[2:]
'7b'
>>>