0
所以我有一个文件,我想通过走线并更换一些词是非常相似,实际上他们中的一些人的字符串:智能代替重复的值蟒蛇
uint8=("UINT8 ","PIN_UINT8 ")
uint16=("UINT16 ","PIN_UINT16 ")
uint32=("UINT32 ","PIN_UINT32 ")
uint64=("UINT64 ","PIN_UINT64 ")
int8=("INT8 ","PIN_INT32 ")
int16=("INT16 ","PIN_INT16 ")
int32=("INT32 ","PIN_INT32 ")
int64=("INT64 ","PIN_INT64 ")
这些都是替代品。但我需要小心,因为INT8
它的子串UINT8
所以即使我有一场比赛,我需要检查它是否是UINT8
或INT8
。
我写了一些代码,但远没有工作。
while pos!=-1:
print line[pos:]
if "INT8 " in line[pos:]:
pos=line.find("INT8",pos_final)
if "U" == line[pos-1]:
line = line.rstrip().replace(*uint8)
else:
print line[pos-1]
line = line.rstrip().replace(*int8)
pos+=5 #len(PIN_)+1
elif "INT16 " in line[pos:]:
pos=line.find("INT16",pos_final)
if "U" == line[pos-1]:
line = line.rstrip().replace(*uint16)
else:
print line[pos-1]
line = line.rstrip().replace(*int16)
pos+=5
elif "INT32 " in line[pos:]:
pos=line.find("INT32",pos_final)
if "U" == line[pos-1]:
line = line.rstrip().replace(*uint32)
else:
print line[pos-1]
line = line.rstrip().replace(*int32)
pos+=5
elif "INT64 " in line[pos:]:
pos=line.find("INT64",pos_final)
if "U" == line[pos-1]:
line = line.rstrip().replace(*uint64)
else:
print line[pos-1]
line = line.rstrip().replace(*int64)
pos+=5
else:
pos =-1
在这里有该文件的某些行这样你就可以知道,如果反应是正确的,工作原理:
UINT32 CacheSize() const { return _cacheSize; } UINT32 LineSize() const { return _lineSize; } UINT32 Associativity(UINT64 obj, INT8 obj2) const { return _associativity; } VOID SplitAddress(const ADDRINT addr, CACHE_TAG & tag, UINT32 & setIndex) const VOID SplitAddress(const ADDRINT addr, CACHE_TAG & tag, UINT32 & setIndex, UINT32 & lineIndex) const { const UINT32 lineMask = _lineSize - 1;
只是做了UINT-替代的INT-替换之前,它应该工作... – L3viathan
也许使用正则表达式 - 're'模块 – furas
@ L3viathan改变''INT8到''PIN_INT8它会改变'PIN_UINT8 '到'PIN_UPIN_INT8' – furas