cshenfu
2023-03-14 1c6213c73eb6c681b406d5a69b80ebff10b29b23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
L = []
a = b'\x00\xef\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x19\x39\xd9\x9d\xfdthe first line\n\r\a\b\t\\\f\'\"\v\b\n\000'
print(a)
for each in a:
    L.append(int(each))
with open('data.txt','w') as p:
    p.write(str(L))
print(L)
 
 
with open('data.txt','r') as p:
    line = p.readline()
print(b''.join([bytes([int(i)]) for i in line[1:-1].split(',')]))
 
 
def readbytetxt2(filename):
    dic = {
    '0': 0,    '1': 1,    '2': 2,
    '3': 3,    '4': 4,    '5': 5,
    '6': 6,    '7': 7,    '8': 8,
    '9': 9,    'a': 10,   'b': 11,
    'c': 12,   'd': 13,   'e': 14,
    'f': 15,
    }
    dic2 = {
    'a': '\a',     'b': '\b', 
    'f': '\f',     'n': '\n', 
    'r': '\r',     'v': '\v', 
    '\'': '\'',    '\"': '', 
    '\\': '\\', 
    }
    with open(filename,'r') as p:
        line = p.readline()
        while line:
            if line[-1] == '\n':
                line = line[:-1]
            i = 2
            L = b''
            while i+1 < len(line):
                if line[i:i+2] == '\\x' and (line[i+2] in dic.keys()) and (line[i+3] in dic.keys()):
                    L += bytes([dic[line[i+2]]*16+dic[line[i+3]]])
                    i += 4
                elif line[i] == '\\' and line[i+1] in dic2.keys():
                    L += bytes(dic2[line[i+1]],'utf8')
                    i += 2
                elif line[i:i+4] == '\\000':
                    L += bytes('\000','utf8')
                    i += 2
                else:
                    L += bytes(line[i],'utf8')
                    i += 1
            yield L
            line = p.readline()
 
a = b'\x00\xef\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x19\x39\xd9\x9d\xfdthe first line\n\r\a\b\t\\\f\'\"\v\b\n\000'
b = b'\xa0\xdf\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x19\x39\xd9\x9d\xfdthe second line\nn'
c = b'\xe0\xaf\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x19\x39\xd9\x9d\xfdthe third line\\'
with open('data.txt','w') as p:
    p.write(str(a)+'\n')
    p.write(str(b)+'\n')
    p.write(str(c))
 
line = readbytetxt2('data.txt')
 
print([a for a in line])