排除 python 中的非 ASCII 字符
excluding non-ascii characters in python
我有一个使用字典来解密加密消息的脚本,问题是解密过程会产生大量垃圾(a.k.a 非 ascii)字符。这是我的代码:
from Crypto.Cipher import AES
import base64
import os
BLOCK_SIZE = 32
PADDING = '{'
# Encrypted text to decrypt
encrypted = "WI4wBGwWWNcxEovAe3p+GrpK1GRRQcwckVXypYlvdHs="
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
adib = open('words.txt')
for line in adib.readlines():
secret = line.rstrip('\n')
if (secret[-1:] == "\n"):
print "Error, new line character at the end of the string. This will not match!"
elif (len(secret) >= 32):
print "Error, string too long. Must be less than 32 characters."
else:
# create a cipher object using the secret
cipher = AES.new(secret + (BLOCK_SIZE - len(secret) % BLOCK_SIZE) * PADDING)
# decode the encoded string
decoded = DecodeAES(cipher, encrypted)
print decoded+"\n"
到目前为止,我想到的是将 decoded
字符串转换为 Ascii,然后排除非 ASCII 字符,但它没有用。
您可以像这样删除非 ascii 字符:
编辑:先用解码更新。
output = 'string with some non-ascii characters��@$���9�HK��F�23 some more char'
output = output.decode('utf-8').encode('ascii', 'ignore')
这个版本可以工作:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
def evaluate_string_is_ascii(mystring):
is_full_ascii=True
for c in mystring:
try:
if ord(c)>0 and ord(c)<=127:
#print c,"strict ascii =KEEP"
pass
elif ord(c)>127 and ord(c)<=255:
#print c,"extended ascii code =TRASH"
is_full_ascii=False
break
else:
# print c,"no ascii =TRASH"
is_full_ascii=False
break
except:
#print c,"no ascii =TRASH"
is_full_ascii=False
break
return is_full_ascii
my_text_content="""azertwxcv
123456789
456dqsdq13
o@��nS��?t#�
lkjal�
kfldjkjl&é"""
for line in my_text_content.split('\n'):
#check if line contain only ascii
if evaluate_string_is_ascii(line)==True:
#print the line
print line
if six.PY2:
if isinstance(input_data, str):
input_data = input_data.decode('ascii', 'ignore').encode('ascii')
else:
input_data = input_data.encode('ascii', 'ignore')
else:
six.PY3
input_data = str(input_data)
print(input_data)
我有一个使用字典来解密加密消息的脚本,问题是解密过程会产生大量垃圾(a.k.a 非 ascii)字符。这是我的代码:
from Crypto.Cipher import AES
import base64
import os
BLOCK_SIZE = 32
PADDING = '{'
# Encrypted text to decrypt
encrypted = "WI4wBGwWWNcxEovAe3p+GrpK1GRRQcwckVXypYlvdHs="
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
adib = open('words.txt')
for line in adib.readlines():
secret = line.rstrip('\n')
if (secret[-1:] == "\n"):
print "Error, new line character at the end of the string. This will not match!"
elif (len(secret) >= 32):
print "Error, string too long. Must be less than 32 characters."
else:
# create a cipher object using the secret
cipher = AES.new(secret + (BLOCK_SIZE - len(secret) % BLOCK_SIZE) * PADDING)
# decode the encoded string
decoded = DecodeAES(cipher, encrypted)
print decoded+"\n"
到目前为止,我想到的是将 decoded
字符串转换为 Ascii,然后排除非 ASCII 字符,但它没有用。
您可以像这样删除非 ascii 字符: 编辑:先用解码更新。
output = 'string with some non-ascii characters��@$���9�HK��F�23 some more char'
output = output.decode('utf-8').encode('ascii', 'ignore')
这个版本可以工作:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
def evaluate_string_is_ascii(mystring):
is_full_ascii=True
for c in mystring:
try:
if ord(c)>0 and ord(c)<=127:
#print c,"strict ascii =KEEP"
pass
elif ord(c)>127 and ord(c)<=255:
#print c,"extended ascii code =TRASH"
is_full_ascii=False
break
else:
# print c,"no ascii =TRASH"
is_full_ascii=False
break
except:
#print c,"no ascii =TRASH"
is_full_ascii=False
break
return is_full_ascii
my_text_content="""azertwxcv
123456789
456dqsdq13
o@��nS��?t#�
lkjal�
kfldjkjl&é"""
for line in my_text_content.split('\n'):
#check if line contain only ascii
if evaluate_string_is_ascii(line)==True:
#print the line
print line
if six.PY2:
if isinstance(input_data, str):
input_data = input_data.decode('ascii', 'ignore').encode('ascii')
else:
input_data = input_data.encode('ascii', 'ignore')
else:
six.PY3
input_data = str(input_data)
print(input_data)