Skip to content

Instantly share code, notes, and snippets.

@FelixWeichselgartner
Created February 1, 2023 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FelixWeichselgartner/62e20e0747554109dd05996106217db9 to your computer and use it in GitHub Desktop.
Save FelixWeichselgartner/62e20e0747554109dd05996106217db9 to your computer and use it in GitHub Desktop.
convert broken latin1 to utf8 wordpress sql table backups
with open('exp-my-database-utf8.sql', 'r', encoding='utf8') as s:
input = s.read()
output = ''
split_char = 't'
split = input.split(split_char)
corrected = 0
not_corrected = 0
for i in split:
try:
wrong = i.encode('windows-1252')
right = wrong.decode('utf8')
output += right
corrected += 1
except (UnicodeEncodeError, UnicodeDecodeError) as e:
print(i)
output += i
not_corrected += 1
output += split_char
output = output[:-1]
with open('output.sql', 'w', encoding='utf8') as o:
o.write(output)
print(f'corrected {corrected} segments')
print(f'couldnt corrected {not_corrected} segments')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment