【问题标题】:excluding non-ascii characters in python排除python中的非ASCII字符
【发布时间】:2016-03-10 11:10:26
【问题描述】:

我有一个使用字典解密加密消息的脚本,问题是解密过程会产生大量垃圾(也称为非 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 字符,但它不起作用。

【问题讨论】:

  • 能否请您提供一个“words.txt”文件内容示例
  • 包含常用词,但这里有一些词
  • the and One Piece Episode Chapter Pirates Arc The Edit Volume his SLOTNAME Island that Luffy was for with section World Category Special Manga wikipedia Wiki Encyclopedia are Japanese this Anime SBS Vol page BEGIN END Help Wikia Blue Crew from User Buggy Straw Portrait Grand him Pirate New Template Marines they not Hat Devil FLUSH TOP BOXAD Navibox Monkey their Crocodile Down Page Start Shanks have Shichibukai all has Canon Rules wikia AllPages Fruit Zoro Beli Sea name when Image one Usopp Battle Government Guidelines Random
  • 为什么需要删除 ascii ?您可以使用 base64 编码 print base64.b64encode(decoded) 清除解码字符串中的所有“非 ascii”字符
  • 因为终端中所有解码后的字符串的输出都会以段落的形式告诉我答案

标签: python non-ascii-characters


【解决方案1】:

您可以像这样删除非 ascii 字符: 编辑:首先使用解码更新。

output = 'string with some non-ascii characters��@$���9�HK��F�23 some more char'
output = output.decode('utf-8').encode('ascii', 'ignore')

【讨论】:

  • 我收到此输出错误Traceback (most recent call last): File "code.py", line 28, in <module> decoded = decoded.decode('utf-8').encode('ascii', 'ignore') File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError: 'utf8' codec can't decode byte 0x96 in position 0: invalid start byte
【解决方案2】:

这个版本可以工作:

#!/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

【讨论】:

  • 您的代码运行良好,但我想要的是不打印包含非 ascii 字符的行,因此如果 decoded 字符串包含非 ascii 字符,则不会打印
  • 现在可以了吗?您可以在自己的代码中重用 evaluate_string_is_ascii(mystring) 函数,如下所示:if evaluate_string_is_ascii(decoded)==True: print decoded+"\n"
  • 很高兴为您提供帮助。
【解决方案3】:
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)

【讨论】:

  • 本网站上通常不赞成仅使用代码的答案。您能否编辑您的答案以包含一些 cmets 或对您的代码的解释?解释应回答以下问题:它有什么作用?它是如何做到的?它去哪儿了?它如何解决OP的问题?见:How to anwser。谢谢!
猜你喜欢
  • 2016-07-28
  • 1970-01-01
  • 1970-01-01
  • 2020-03-12
  • 2011-02-14
  • 2018-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多