|
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ÍÚcX¶¹��–@0�/â|ß�¦Ây/|(ÃI�:+� D
� ø ,� ,� ,� - ,� à� � à� � � � � � � � ÿÿÿÿC C (� �� ð� h� °� �� X� ¨� ø� @� ˆ� Ð� �� S �� S � ³� Ó� ã� ó� S �� �� �� �� #� À� ³� 3� C� S� c� s� ƒ� À� ã� “� “� � ³� �� ³� � S À� Ð� S à� S À C� ð� �� �� C� � À Ô¿ ¸¿ üÿ@ � � 8 8 � ÿÿÿÿ 6 � 6 . :�� ��h L.�:�����š�H����� H B 8 8 � ÿÿÿÿ� + P + A , .�� .��.�|�. |
�
.�: ���hL
.
�
��˜
H�.��.|�
.
|
��¦� �
��� H , 8 8 � ÿÿÿÿ� - P - + . .�� .��.�|�
.�|
�
.��.�|�
.�|
��¦� �
��� @ � 8 8 � ÿÿÿÿ / P / .�:���� @ � � 8 8 � ÿÿÿÿ� 3 P 3 .�� ��h L�.�:�H����� @
� 8 8 � ÿÿÿÿ� D � D .�:�� ��ž ��� @
8 8 � ÿÿÿÿ� E � E .�:�� ��ž ��� @ % 8 8 � ÿÿÿÿ F � F . :!:"��� @ � 8 8 � ÿÿÿÿ : � : .#:$��� @ � 8 8 � ÿÿÿÿ ; � ; .%:&��� @ � 8 8 � ÿÿÿÿ < � < .':(��� @ � 8 8 � ÿÿÿÿ = � = .):*��� @ � 8 8 � ÿÿÿÿ > � > .+:,��� p � ¸ Ø �� X� €� ¸� � H Ø (
P
x
¨
ø
� H� �� À� ð� �
¨ Ø (¸
è
0�
� �� °� Ð� ø� @� h� °� à� ��� ˆ� À� è� �� P� €� °� ø� (�� °� à� �� X� ˆ� À� à� �� 8�� ˆ� À� ð� � X� ˆ� ÿÿÿÿ � � ÿÿÿÿ � � Q t Q u i c k ÿÿÿÿ� � � T ÿÿÿÿ� � � Q t Q u i c k . T e m p l a t e s ÿÿÿÿ� � � T . D i a l o g B u t t o n B o x ÿÿÿÿ � � 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 ÿÿÿÿ � � c o n t e n t W i d t h ÿÿÿÿ� � � e x p r e s s i o n f o r c o n t e n t W i d t h ÿÿÿÿ � � s p a c i n g ÿÿÿÿ � � p a d d i n g ÿÿÿÿ � � a l i g n m e n t ÿÿÿÿ� � � e x p r e s s i o n f o r a l i g n m e n t ÿÿÿÿ� � � B u t t o n ÿÿÿÿ� � � w i d t h ÿÿÿÿ� � � e x p r e s s i o n f o r w i d t h ÿÿÿÿ� � � 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 ÿÿÿÿ� � � e x p r e s s i o n f o r s p a c i n g ÿÿÿÿ� � � o r i e n t a t i o n ÿÿÿÿ� � � e x p r e s s i o n f o r o r i e n t a t i o n ÿÿÿÿ� � � b o u n d s B e h a v i o r ÿÿÿÿ� � � e x p r e s s i o n f o r b o u n d s B e h a v i o r ÿÿÿÿ� � � s n a p M o d e ÿÿÿÿ� � � e x p r e s s i o n f o r s n a p M o d e ÿÿÿÿ� � � c o n t e n t I t e m ÿÿÿÿ � � R e c t a n g l e ÿÿÿÿ� � � x ÿÿÿÿ� � � y ÿÿÿÿ� � � h e i g h t ÿÿÿÿ� � � e x p r e s s i o n f o r h e i g h t ÿÿÿÿ� � � c o l o r ÿÿÿÿ� � � e x p r e s s i o n f o r c o l o r ÿÿÿÿ
� � b a c k g r o u n d ÿÿÿÿ � � 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 u n t ÿÿÿÿ� � � a v a i l a b l e W i d t h ÿÿÿÿ� � � 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 ÿÿÿÿ� � � i m p l i c i 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 ÿÿÿÿ� � � i m p l i c i 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 ÿÿÿÿ� � � Q t ÿÿÿÿ
� � A l i g n R i g h t ÿÿÿÿ� � � p a r e n t ÿÿÿÿ � � p a l e t t e ÿÿÿÿ� � � w i n d o w ÿÿÿÿ � � c o n t e n t M o d e l ÿÿÿÿ
� � H o r i z o n t a l ÿÿÿÿ � � F l i c k a b l e ÿÿÿÿ � � S t o p A t B o u n d s ÿÿÿÿ
� � S n a p T o I t e m ÿÿÿÿB � � 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 / D i a l o g B u t t o n B o x . q m l � � � X � � � % � � � � � & � � � ' � l œ� � Ü� Ä� � � � ÿÿÿÿ T T T T T T � ,� ( � ) P 0� 0� & € � � A P A ��� € � � 9 P 9 �� � � 5 P 5 ð � � 3 P 3 �
� � 2 P 2 à � 1 P 1 à
� / P / 0�� � - P - P�� � + P + @� � ÿÿÿÿÿÿ T T T T T � T l 5 ð l l � 6 � 6 � � ÿÿÿÿÿÿ T T T T T � T Ì 9 � Ì Ì � � � > � > 0�� � � � = � = ��� � � < � < `� ; � ; �� � : � : � � ÿÿÿÿÿÿ T T T T T � T ä A �� ä ä $ F � F �" � E � E ��� � D � D �! � C ð C � � C � C À � � � B � B �� ( � ÿÿÿÿÿÿ T T T T T � T l 5 ð l l � � � 5 P 5 ð