|
import sys |
|
import os |
|
import argparse |
|
|
|
placeholder = 'Paste mojibake here\nここで文字化けをペースト下さい' |
|
default_source_enc = 'sjis' |
|
|
|
# Open a file in binary mode and overwrite contents |
|
# with the text encoded in the given encoding |
|
def write_file(filename, encoding, text): |
|
with open(filename, 'wb') as f: |
|
f.write(text.encode(encoding)) |
|
f.write(b'\n') # newline at end of file |
|
|
|
# Open a file and return a string of the file contents decoded using the specified encoding |
|
def read_file(filename, encoding): |
|
with open(filename, 'r', encoding=encoding) as f: |
|
return f.read() |
|
|
|
if __name__ == '__main__': |
|
parser = argparse.ArgumentParser(description='Fix mojibake characters') |
|
parser.add_argument('filename', metavar='file', |
|
help='file to convert') |
|
parser.add_argument('--create', dest='create_file', action='store_true', |
|
help='create the file to paste mojibake text into, and open it in TextEdit') |
|
parser.add_argument('--overwrite', dest='overwrite_file', action='store_true', |
|
help='(only with --create) overwrite file') |
|
parser.add_argument('--from', dest='from_encoding', |
|
metavar='source_encoding', |
|
default=default_source_enc, |
|
help=f'source encoding (default: {default_source_enc})') |
|
|
|
args = parser.parse_args() |
|
|
|
from_encoding = args.from_encoding |
|
# target_encoding = 'utf-8' |
|
filename = args.filename |
|
create_file = args.create_file |
|
overwrite_file = args.overwrite_file |
|
|
|
if create_file: |
|
if os.path.exists(filename): |
|
if not overwrite_file: |
|
print('File already exists, skipping...') |
|
else: |
|
# 1. Write placeholder text to file, in original encoding |
|
print('Writing placeholder text to file') |
|
write_file(filename, from_encoding, placeholder) |
|
|
|
# 2. Paste manually into TextEdit |
|
print('Please paste the text manually into TextEdit') |
|
print('Press enter when ready to proceed') |
|
os.system('open -a TextEdit ' + filename) |
|
input() |
|
|
|
# 3. Output correctly-decoded file contents |
|
# New encoding should be in utf-8, which (I think) usually corresponds to what Python uses to output and is what most terminals use by default |
|
converted = read_file(filename, from_encoding) |
|
print(converted) |
qv4cdata) ��� ‰ˆ¯Ñ“� <� ¢¤þ,à�Ùþ±ÂKðq�«øe—e’wš—èÓ½<µ¸¹ŠŒ� > ˜�
ø � � � / � Ü� � à� �� �� �� �� �� �� �� ÿÿÿÿ= = h� �� h� À� 8� °� � P� ˜� à� P� c� ƒ €� �� p� c� ƒ €� �� p� £� Ã� Ó� ã� ó� �� �� °� £� #� 3� C� S� c� s� °� ƒ €� €� ƒ €� �� S� ƒ � ³� €� S� ƒ
� ƒ p� ³� €� À� ƒ p� üÿ À •¿ ¸¿¸�…ëQ¸BÀ �À@ � � 8 8 � ÿÿÿÿ P � P . � .�:�:�� �� ¦� � ��� @ � � 8 8 � ÿÿÿÿ L � L .�� .�: :�� �� ¦ � ��� H , 8 8 � ÿÿÿÿ� . P . + / . � . �.|� . | � .��.�|� .�| � ¦� � ��� H , 8 8 � ÿÿÿÿ� 0 P 0 + 1 .�� .��.�|� .�| � .��.�|� .�| � ¦� � ��� @ � 8 8 � ÿÿÿÿ G � G .�:�:���� @ � 8 8 � ÿÿÿÿ H � H .�:�:���� @ � 8 8 � ÿÿÿÿ 9 � 9 . ��� @ � 8 8 � ÿÿÿÿ : � : .!:"��� H ( � 8 8 � ÿÿÿÿ ; � ; ' = .#:$L�.%� .&:'| ��.(:)|�� .*:+:,H����@ � 8 8 � ÿÿÿÿ ? � ? .-:.��� �� °� Ø� �
€ À ø � H� €� Ø� � h � ¸ è � H p ¸ è 8˜
è
P € ° Ø
H
x
¨
Ð
� 8� €� ¨� Ø� � (� P� p� ¸� è� �� P� €� ¸� �� 8� h� � Ð� �� 0� X� �� ¸� à� ÿÿÿÿ � � ÿÿÿÿ � � Q t Q u i c k ÿÿÿÿ� � � Q t Q u i c k . C o n t r o l s ÿÿÿÿ� � � Q t Q u i c k . C o n t r o l s . i m p l ÿÿÿÿ� � � T ÿÿÿÿ� � � Q t Q u i c k . T e m p l a t e s ÿÿÿÿ� � � Q t Q u i c k . W i n d o w ÿÿÿÿ� � � T . M e n u ÿÿÿÿ � � c o n t r o l ÿÿÿÿ
� � i m p l i c i t W i d t h ÿÿÿÿ� � � e x p r e s s i o n f o r i m p l i c i t W i d t h ÿÿÿÿ� � � i m p l i c i t H e i g h t ÿÿÿÿ� � � e x p r e s s i o n f o r i m p l i c i t H e i g h t ÿÿÿÿ � � m a r g i n s ÿÿÿÿ � � o v e r l a p ÿÿÿÿ� � � M e n u I t e m ÿÿÿÿ� � � d e l e g a t e ÿÿÿÿ� � � L i s t V i e w ÿÿÿÿ� � � m o d e l ÿÿÿÿ� � � e x p r e s s i o n f o r m o d e l ÿÿÿÿ
� � i n t e r a c t i v e ÿÿÿÿ� � � e x p r e s s i o n f o r i n t e r a c t i v e ÿÿÿÿ� � � c l i p ÿÿÿÿ � � c u r r e n t I n d e x ÿÿÿÿ� � � e x p r e s s i o n f o r c u r r e n t I n d e x ÿÿÿÿ� � � S c r o l l I n d i c a t o r ÿÿÿÿ� � � v e r t i c a l ÿÿÿÿ
� � c o n t e n t I t e m ÿÿÿÿ � � R e c t a n g l e ÿÿÿÿ� � � c o l o r ÿÿÿÿ� � � e x p r e s s i o n f o r c o l o r ÿÿÿÿ� � � b o r d e r ÿÿÿÿ
� � b a c k g r o u n d ÿÿÿÿ � � T . O v e r l a y ÿÿÿÿ� � � m o d a l ÿÿÿÿ� � � m o d e l e s s ÿÿÿÿ � � Q m l I n t e r n a l s ÿÿÿÿ� � � Q m l I n t e r n a l s . C o m p o n e n t ÿÿÿÿ� � � C o l o r ÿÿÿÿ
� � t r a n s p a r e n t ÿÿÿÿ � � p a l e t t e ÿÿÿÿ� � � s h a d o w ÿÿÿÿ� � � M a t h ÿÿÿÿ� � � m a x ÿÿÿÿ� � � i m p l i c i t B a c k g r o u n d W i d t h ÿÿÿÿ � � l e f t I n s e t ÿÿÿÿ
� � r i g h t I n s e t ÿÿÿÿ � � c o n t e n t W i d t h ÿÿÿÿ
� � l e f t P a d d i n g ÿÿÿÿ � � r i g h t P a d d i n g ÿÿÿÿ� � � i m p l i c i t B a c k g r o u n d H e i g h t ÿÿÿÿ� � � t o p I n s e t ÿÿÿÿ
� � b o t t o m I n s e t ÿÿÿÿ
� � c o n t e n t H e i g h t ÿÿÿÿ
� � t o p P a d d i n g ÿÿÿÿ
� � b o t t o m P a d d i n g ÿÿÿÿ� � � w i n d o w ÿÿÿÿ� � � d a r k ÿÿÿÿ � � c o n t e n t M o d e l ÿÿÿÿ� � � W i n d o w ÿÿÿÿ� � � h e i g h t ÿÿÿÿ7 � � q r c : / q t - p r o j e c t . o r g / i m p o r t s / Q t Q u i c k / C o n t r o l s . 2 / M e n u . q m l � �
� � � % � � � � & � � � � ' � � � � � ( � � � � ) � � � $ � Ô ì� D� ,� „� ô� ¬� �� Œ� �� „� ô� d � � ÿÿÿÿ T T T T T � T � �� + � , P �� �� € � � D P D ��� € � � 8 P 8 �� �
6 P 6 ð � � � 4 P 4 à
� 3 P 3 à
� 0 P 0 P� � . P . @�! � K p K ð � ÿÿÿÿÿÿ T T T T T T T 6 ð T T � ÿÿÿÿÿÿ T T T T T � T ä 8 � ä ä � ? � ? p�� � � > � > ð � � ; � ;
�� : � : � � 9 � 9 ��� � A � A �� � ÿÿÿÿÿÿ T T T T T T T A 0� T T ÿÿÿÿÿÿ T T T T T � T l l l � � � A ��A 0� � ÿÿÿÿÿÿ T T T T T � T ´ D �� ´ ´ � � G � G � � � F � F �� � � E � E €�� � H � H � ÿÿÿÿÿÿ T T T T T � T l l l � � H �H p� � ÿÿÿÿÿÿ T T T T T � T l K� l l � � L � L � ÿÿÿÿÿÿ T T T T T � T „ „ „ # �O ð O ��" � K ð K
� � ÿÿÿÿÿÿ T T T T T � T l O �� l l � P � P � % � ÿÿÿÿÿÿ T T T T T � T l 6 ð l l � � � 6 P 6 ð % � ÿÿÿÿÿÿ T T T T T � T l O �� l l # � O ð O �� % � ÿÿÿÿÿÿ T T T T T � T l K� l l " � K ð K `�