데이터를 문자 단위로 처리하고 매핑을 찾으면 대체하는 코드입니다. 이것은 교체해야 할 각 데이터가 절대적으로 고유하다고 가정합니다. 귀하의 예제 데이터에
def replacer(instring, mapping):
item = ''
for char in instring:
item += char
yield item[:-5]
item = item[-5:]
if item in mapping:
yield mapping[item]
item = ''
yield item
old_values = ('0000}', '0000J', '0000K', '0000L', '0000M', '0000N')
new_values = (' -0', ' -1', ' -2', ' -3', ' -4', ' -5')
value_map = dict(zip(old_values, new_values))
file_snippet = '00000000000000010000}0000000000000000000200002000000000000000000030000J0000100000000000000500000000000000000000000' # each line is >7K chars long and there are over 6 gigs of text data
result = ''.join(replacer(file_snippet, value_map))
print result
이 제공 :
old_values = ('0000}', '0000J', '0000K', '0000L', '0000M', '0000N')
new_values = (' -0', ' -1', ' -2', ' -3', ' -4', ' -5')
value_map = dict(zip(old_values, new_values))
file_snippet = '00000000000000010000}0000000000000000000200002000000000000000000030000J0000100000000000000500000000000000000000000' # each line is >7K chars long and there are over 6 gigs of text data
result = []
for chunk in [ file_snippet[i:i+5] for i in range(0, len(file_snippet), 5) ]:
if chunk in value_map:
result.append(value_map[chunk])
else:
result.append(chunk)
result = ''.join(result)
print result
:
0000000000000001 -0000000000000000000020000200000000000000000003 -10000100000000000000500000000000000000000000
더 빠른 방법은 데이터가 그런 식으로 맞는 경우, 5 문자 조각으로 데이터를 분할하는 것 선행 0을 제거하지 않으면 결과 데이터가 대체되지 않습니다.
000000000000001 -0000000000000000000020000200000000000000000003 -10000100000000000000500000000000000000000000
위와 동일합니다.
현재 속도가 느린 방법을 게시하십시오. –
'더 많은'이 몇 개나됩니까? 그들은 모두 같은 길이입니까? 그들은 모두 같은 길이로 오프셋되어 있습니까? –
변경하려는 경우 결국 데이터를 반복해야합니다. –