简体   繁体   English

在 Python 中生成用于条码项目的序列

[英]Generate a sequence in Python for barcoding items

I am trying to generate barcodes in an app to tag the products which includes 3 things:我正在尝试在应用程序中生成条形码来标记产品,其中包括 3 件事:

  1. Batch no.批号。 (GRN ID) (GRN ID)
  2. Product ID产品编号
  3. serial ID序列号

Something like this:像这样的东西:

 def get(self, request, *args, **kwargs):
        pk = self.kwargs['pk']
        grn = Grn.objects.filter(pk=pk)[0]
        grn_prod = grn.items.all()
        items = []
        for i in grn_prod:
            for j in range(i.item_quantity):
                items.append("YNT" + str(pk) + str(i.item.pk) + str(j + 1))  

It generates a sequence like the following:它生成如下序列:

YNT55232

Which is good but while scanning it if I want to know the item ID or Serial ID the it becomes a problem as it could be 23, 523, 3, etc.这很好,但是在扫描它时,如果我想知道item IDSerial ID ,它就会成为一个问题,因为它可能是 23、523、3 等。

For this I want to specify a no of digits for GRN , Product and Serial Id something like this:为此,我想为GRNProductSerial Id指定一个数字,如下所示:

GRN Barcode     GRN ID  Product ID  Serial ID
            YNT  000X     000X       0000X

I am unable to figure out how to append 0 before the IDs ?我无法弄清楚在IDs之前如何 append 0

you can use format in Python.您可以使用 Python 中的format It is commonly used to format many variables.它通常用于格式化许多变量。

If you want to format this: "YNT" + str(pk) + str(i.item.pk) + str(j + 1) you can use format as below:如果你想格式化这个: "YNT" + str(pk) + str(i.item.pk) + str(j + 1)你可以使用如下format

'"YNT"\t{:04d}\t{:04d}\t{:05d}'.format(pk, i.item.pk, j+1)

In case you do not know;如果您不知道; the {} are for each variable as in order in format() . {}用于每个变量,如format()中的顺序。

As you want to have pk and i.item.pk as four characters, then you add :04d .由于您希望将pki.item.pk作为四个字符,因此添加:04d :04d completes the words with 0. For instance; :04d用 0 完成单词。例如;

if pk = 1 , then it converts it to 0001 , or if it is 101 then it converts to 0101 .如果pk = 1 ,则将其转换为0001 ,或者如果为101 ,则将其转换为0101

Same is for j+1 , if j+1 is 1, then it generates 00001 , if it is 101 , then it generates 00101 . j+1也是如此,如果j+1为 1,则生成00001 ,如果为101 ,则生成00101

If you have not used format in Python, I suggest you learn it.如果你没有使用过Python中的格式,建议你学习一下。 It is really helpful for formatting variables.这对格式化变量非常有帮助。

Thezfill function does exactly this. zfill function 正是这样做的。 str.zfill(5) will pad given string in variable str to at least 5 characters, for example. str.zfill(5)会将变量str中的给定字符串填充到至少 5 个字符。

There are different ways to achieve String formatting .有不同的方法来实现字符串格式化

%-formatting %-格式化

Look at the above mention documentation of string function zfill .查看上面提到的字符串 function zfill的文档。 A box follows that explains:后面的方框解释:

printf style String Formatting using the % operator (modulo): printf使用%运算符(取模)进行字符串格式化:

barcode = '%(gnr)03d%(product)03d%(serial)04d' % {'gnr': 123, 'product': 456, 'serial': 7890}

print(barcode)

produces YNT1234567890 .产生YNT1234567890

f-strings (since Python 3.6) f 弦(自 Python 3.6 起)

You can also use the Python 3 way with f-strings :您还可以将 Python 3 方式与f-strings一起使用:

# barcode components readable named
gnr = pk
product = i.item.pk
serial = j + 1

#  format-literal simply will replace the variables named
barcode = f"YNT{gnr:03}{product:03}{serial:04}"

items.append(barcode)

It uses a prefix after the variable-name:它在变量名之后使用前缀:

  • :0x for left-padding with x leading zeros. :0x用于左填充x前导零。

Note: I always clearly named the template-variables (here: barcode components):注意:我总是清楚地命名模板变量(这里:条形码组件):

  • put in a map with descriptive keys (like above in %-formatting)放入带有描述性键的 map(如上面的 % 格式)
  • put in separate variables with names describing each of them (like in f-string example)放入单独的变量,其名称描述它们中的每一个(如在 f-string 示例中)

So we can use a readable template also called format-literal like: "{component_1} before {component_2} then {the_rest}"所以我们可以使用一个可读的模板,也称为格式文字,例如: "{component_1} before {component_2} then {the_rest}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM