python编码器下载_Python Code128编码器

这是一个可以接受的答案,所以我将原始代码留在下面。但我更喜欢这种精致。在def list_join(seq):

''' Join a sequence of lists into a single list, much like str.join

will join a sequence of strings into a single string.

'''

return [x for sub in seq for x in sub]

code128B_mapping = dict((chr(c), [98, c+64] if c < 32 else [c-32]) for c in range(128))

code128C_mapping = dict([(u'%02d' % i, [i]) for i in range(100)] + [(u'%d' % i, [100, 16+i]) for i in range(10)])

code128_chars = u''.join(chr(c) for c in [212] + list(range(33,126+1)) + list(range(200,211+1)))

def encode128(s):

''' Code 128 conversion for a font as described at

https://en.wikipedia.org/wiki/Code_128 and downloaded

from http://www.barcodelink.net/barcode-font.php

Only encodes ASCII characters, does not take advantage of

FNC4 for bytes with the upper bit set. Control characters

are not optimized and expand to 2 characters each.

Coded for https://stackoverflow.com/q/52710760/5987

'''

if s.isdigit() and len(s) >= 2:

# use Code 128C, pairs of digits

codes = [105] + list_join(code128C_mapping[s[i:i+2]] for i in range(0, len(s), 2))

else:

# use Code 128B and shift for Code 128A

codes = [104] + list_join(code128B_mapping[c] for c in s)

check_digit = (codes[0] + sum(i * x for i,x in enumerate(codes))) % 103

codes.append(check_digit)

codes.append(106) # stop code

return u''.join(code128_chars[x] for x in codes)

^{pr2}$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值