纸条上的值错误

2024-09-30 04:39:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我是个业余程序员。每次我运行脚本时

invalid literal for int() with base 10

来了。你知道吗

该代码用于将dumchar\u信息文件散点.txt. 你知道吗

代码来自this site

import sys

import string

import re


ins = open( "dumchar.txt", "rb" )

outs = open( "scatter.txt", "wb" )

for line in ins:

    linesp = re.split('\W+', line)

    name = linesp[0].upper()

    start = int(linesp[2],16)

    block = linesp[5]

    if block != 'misc':

        start = start + 0x600000

    outs.write(name + " " + string.replace(hex(start), "L", "") + "\n{\n}\n")

ins.close()

outs.close()

Tags: 代码nameimportretxtforstringline
2条回答

问题出在文件中,因为它以您期望的格式开始,但是当您继续读取它时,格式在最后5行中发生了变化。我说的是:

usrdata      0x000000013cc80000   0x0000000093300000   2   /dev/block/mmcblk0p7
bmtpool      0x0000000001500000   0x00000000febf00a8   2   /dev/block/mmcblk0
Part_Name:Partition name you should open;
StartAddr:Start Address of partition;
Type:Type of partition(MTD=1,EMMC=2)
MapTo:actual device you operate
Size:size of partition

这正是它的变化,从有数字,你要找的只是描述(部分)_名称:分区名称。。。). 您可以更改文件以删除最后5行,也可以更改脚本,使其在不再具有有效输入时停止读取文件。你知道吗

你还需要删掉标题的第一行,因为这也会弄乱格式。所以这条线

Part_Name       Size    StartAddr       Type    MapTo

看到了吗丹尼斯。弗里德曼的答案。你知道吗

问题是linesp[2]的格式并不总是您所期望的格式。正如Kamal Sadek正确指出的,问题在于文件的第一行和最后五行,其中linesp[2]不是十六进制数。你知道吗

解决这个问题的方法是增加

if not linesp[2].startswith('0x'):
    continue

就在之前

start = int(linesp[2],16)

相关问题 更多 >

    热门问题