简体   繁体   English

字符串,整数和前导零

[英]Strings, ints and leading zeros

I need to record SerialNumber(s) on an object. 我需要在一个对象上记录SerialNumber。 We enter many objects. 我们输入许多物体。 Most serial numbers are strings - the numbers aren't used numerically, just as unique identifiers - but they are often sequential. 大多数序列号都是字符串 - 数字不是数字使用的,就像唯一标识符一样 - 但它们通常是顺序的。 Further, leading zeros are important due to unique id status of serial number. 此外,由于序列号的唯一ID状态,前导零是重要的。

When doing data entry, it's nice to just enter the first "sequential" serial number (eg 000123) and then the number of items (eg 5) to get the desired output - that way we can enter data in bulk see below: 在进行数据输入时,最好只输入第一个“顺序”序列号(例如000123),然后输入项目数(例如5个)以获得所需的输出 - 这样我们可以批量输入数据,如下所示:

Obj1.serial = 000123
Obj2.serial = 000124
Obj3.serial = 000125
Obj4.serial = 000126
Obj5.serial = 000127

The problem is that when you take the first number-as-string, turn to integer and increment, you loose the leading zeros. 问题是当你取第一个数字作为字符串,转向整数和增量时,你松开前导零。

Not all serials are sequential - not all are even numbers (eg FDM-434\\RRTASDVI908) 并非所有序列都是连续的 - 并非所有序列都是偶数(例如FDM-434 \\ RRTASDVI908)

But those that are, I would like to automate entry. 但那些,我想自动进入。

In python, what is the most elegant way to check for leading zeros (*and, I guess, edge cases like 0009999) in a string before iterating, and then re-application of those zeros after increment? 在python中,在迭代之前在字符串中检查前导零(*和,我猜,像0009999这样的边缘情况)的最优雅方法是什么,然后在增量后重新应用这些零?

I have a solution to this problem but it isn't elegant. 我有一个解决这个问题的方法,但它并不优雅。 In fact, it's the most boring and blunt alg possible. 事实上,它是最无聊和最直接的alg。

Is there an elegant solution to this problem? 这个问题有优雅的解决方案吗?

EDIT 编辑

To clarify the question, I want the serial to have the same number of digits after the increment. 为了澄清这个问题,我希望序列在增量具有相同的位数。

So, in most cases, this will mean reapplying the same number of leading zeros. 因此,在大多数情况下,这将意味着重新应用相同数量的前导零。 BUT in some edge cases the number of leading zeros will be decremented. 但在某些边缘情况下,前导零的数量将减少。 eg: 009 -> 010; 例如:009 - > 010; 0099 -> 0100 0099 - > 0100

Try str.zfill() : 试试str.zfill()

>>> s = "000123"
>>> i = int(s)
>>> i
123
>>> n = 6
>>> str(i).zfill(n)
'000123'

You could check the length of the string ahead of time, then use rjust to pad to the same length afterwards: 您可以提前检查字符串的长度,然后使用rjust填充到相同的长度:

>>> s = "000123"
>>> len_s = len(s)
>>> i = int(s)
>>> i
123
>>> str(i).rjust(len_s, "0")
'000123'

You can check a serial number for all digits using: 您可以使用以下方法检查所有数字的序列号:

if serial.isdigit():

I develop my comment here, Obj1.serial being a string: 我在这里开发我的评论, Obj1.serial是一个字符串:

Obj1.serial = "000123"

('%0'+str(len(Obj1.serial))+'d') % (1+int(Obj1.serial))

It's like @owen-s answer '%06d' % n : print the number and pad with leading 0. 这就像@ owen -s的回答'%06d' % n :打印数字并用前导0填充。

Regarding '%d' % n , it's just one way of printing . 关于'%d' % n ,它只是一种打印方式 From PEP3101 : 来自PEP3101

In Python 3.0, the % operator is supplemented by a more powerful string formatting method, format(). 在Python 3.0中,%运算符由更强大的字符串格式化方法format()补充。 Support for the str.format() method has been backported to Python 2.6. 对str.format()方法的支持已经被反向移植到Python 2.6。

So you may want to use format instead… Anyway, you have an integer at the right of the % sign, and it will replace the %d inside the left string. 所以你可能想要使用格式...无论如何,你在%符号的右边有一个整数,它将替换左边字符串中的%d

'%06d' means print a minimum of 6 (6) digits (d) long, fill with 0 (0) if necessary. '%06d'表示打印最少6(6)位(d)长,必要时填写0(0)。

As Obj1.serial is a string, you have to convert it to an integer before the increment: 1+int(Obj1.serial) . 由于Obj1.serial是一个字符串,您必须在增量之前将其转换为整数: 1+int(Obj1.serial) And because the right side takes an integer, we can leave it like that. 因为右边是一个整数,我们可以这样做。

Now, for the left part, as we can't hard code 6, we have to take the length of Obj1.serial . 现在,对于左边部分,由于我们不能硬编码6,我们必须采用Obj1.serial的长度。 But this is an integer, so we have to convert it back to a string, and concatenate to the rest of the expression %0 6 d : '%0'+str(len(Obj1.serial))+'d' . 但这是一个整数,所以我们必须将它转换回一个字符串,并连接到表达式的其余部分%0 6 d'%0'+str(len(Obj1.serial))+'d' Thus 从而

('%0'+str(len(Obj1.serial))+'d') % (1+int(Obj1.serial))

Now, with format ( format-specification ): 现在,使用格式( 格式规范 ):

'{0:06}'.format(n)

is replaced in the same way by 以相同的方式替换

('{0:0'+str(len(Obj1.serial))+'}').format(1+int(Obj1.serial))

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

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