Я понимаю, что вы хотите сохранить такое же количество столбцов, как в заголовке, в этом случае я сделаю что-то вроде этого:
outputlines = []
# Replace the undesired characters in each line
with open("yourFileNameHere","r") as reader:
lines = reader.readlines()
for line in lines:
# Get the length of each line in order to determine which logic to apply
if len(line.split("\t")) > 3:
outputlines.append(line.strip().replace("\t\t","|").replace("\t","|"))
else:
outputlines.append(line.strip().replace("\t","|"))
# Write back the file with the new format
with open("myOutputFileName","w") as writer:
rows = len(outputlines)
for i in range(rows):
if i != rows - 1:
writer.write(f'{outputlines[i]}\n')
else:
writer.write(f'{outputlines[i]}')
Пожалуйста, дайте мне знать, если это поможет: D!