Life Since 1985

Happy Hacking

115诡异的乱码

david kei posted @ 2010年8月05日 17:56 in Log with tags python 乱码 , 1512 阅读

今天下载115网盘的资源,文件名一堆乱码,以前就出现过这种状况。其实也不是乱码,仔细看了一下应该是用%分隔的unicode码。反正也是闲的没事自己就用python写了一个恢复正常文件名的小程序,还算比较简单,主要就是读取文件名,然后将文件名按照%分隔,再手动设置一个溢出判断,超出两个字符就算溢出,溢出的部分和在文件名中的位置偏移另存,然后转换为字符就行了,最后将转换完的文件名存储。

python确实比较方便啊,不过十几二十行的代码而已,要是这个程序用c 或者java写的话……

# -*- coding:gb2312 -*-
F = open('datafile')
s = F.readlines()
s = [c.rstrip() for c in s]
ls = [c.split('%') for c in s]
result  = []
for c in ls:
    del c[0]
    isLarge = False
    sTag = None
    nTag = None
    n = -1
    for i in c:
        n = n + 1
        if len(i) > 2:
            isLarge = True
            sTag = i[2:]
            c[n] = i[:2]
            nTag = n + 1
            #print sTag
            #print nTag
    c = ['0x' + p for p in c]
    c = [eval(p) for p in c]
    c = [chr(p) for p in c]
    if isLarge == True:
        c.insert(nTag, sTag)
    result.append(''.join(c) + '\n')
    #print c

F.close()
F = open('myfile', 'w')
F.writelines(result)
'''
for i in result:
    print i
'''
NCERT Term 1 Questio 说:
2022年9月28日 05:45

Every student who wants to get a high score in their Term-1 Examinations can download and practice NCERT Term-1 Sample Paper 2023 Class 2 and the question paper may include mostly short answer questions and objectives to bring out students’ abilities. NCERT Term 1 Question Paper Class 2 Class teachers and subject experts have suggested revision questions mock test questions along with IMP questions which have been repeatedly asked in previous question Papers.Every student who wants to get a high score in their Term-1 Examinations can download and practice NCERT Term-1 Sample Paper 2023 Class 2 and the question paper may include mostly short answer questions and objectives to bring out students’ abilities.

Alyssa 说:
2022年12月26日 01:09

There's something weird about garbled codes. They don't seem to follow any rules or patterns, and they're often hard to understand. Even experts have trouble diamond rings deciphering them. But that's part of their appeal. They're like puzzles, waiting to be solved. There are all sorts of theories about why garbled codes exist. Some believe they're intentional, while others think they're the result of errors. No one knows for sure. But one thing is certain: they're fascinating, and everyone wants to crack the code.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter