简体   繁体   English

保存以 0 开头的数字时保留前导零

[英]Keep leading zeros when saving numbers that start with 0

I am trying to save a list of site codes, for example:我正在尝试保存站点代码列表,例如:

site_codes = [1302,9033,1103,5005,0016]

Then I want to add the site code to URLs before running web scraping, using site_codes[i] , for example:然后我想在运行网络抓取之前将站点代码添加到 URL,使用site_codes[i] ,例如:

for i in range(len(site_codes)): 
  
  Data_site_A.append("https://.../"+str(parameters[i])+"site="+str(site_codes[0]))
  Data_site_B.append("https://.../"+str(parameters[i])+"site="+str(site_codes[1]))

But I can not save 0016 into the list just like other numbers.但是我不能像其他数字一样将0016保存到列表中。 I have tried many ways including:我尝试了很多方法,包括:

# make a string
str("{0}{1}{2}".format(0,0,16))

# fill the 0
"%04d" % 16

But they all return '0016' instead of 0016 .但它们都返回'0016'而不是0016 So when I input '0016' into the urls, it won't work, because it is not a number.因此,当我在网址中输入'0016' ,它不起作用,因为它不是数字。

Is there a way to save this number just as 0016 ?有没有办法像0016一样保存这个数字? Or since that print("%04d" % 16) will print out a pure 0016 , is there a way to save the output from there?或者因为print("%04d" % 16)会打印出一个纯0016 ,有没有办法从那里保存输出?

For the desired output, the computer should interpret it as:对于所需的输出,计算机应将其解释为:

"https://...."+str(parameters[i])+"site=0016")
# use regular expression

import re

site_codes = '''
         site code:
         site_A: 1302
         site_B: 9033
         site_C: 1103
         site_D: 5005
         site_E: 0016
         '''

site_codes = re.findall(r'\d+',site_codes)

for i in range(len(site_codes)): 
    Data_site_A.append("https://.../"+str(parameters[i])+"site="+str(site_codes[0]))
    Data_site_B.append("https://.../"+str(parameters[i])+"site="+str(site_codes[1]))

Use str.zfill() to add leading zeros to a number;使用str.zfill()为数字添加前导零;
Call str(object) with a number as object to convert it to a string.以数字为对象调用str(object)以将其转换为字符串。
Call str.zfill(width) on the numeric string to pad it with 0 to the specified width.对数字字符串调用str.zfill(width)以将其填充为 0 到指定的宽度。

print(a_number)

OUTPUT=
123

Convert a_number to a stringa_number转换为字符串

number_str = str(a_number)

Pad number_str with zeros to 5 digits用零到 5 位数字填充number_str

zero_filled_number = number_str.zfill(5)

print(zero_filled_number)

OUTPUT=
00123

Assuming that you really do have a list of integers that can't be retained as strings and want to create the URLs.假设您确实有一个不能作为字符串保留的整数列表并且想要创建 URL。 Also assuming that you are using Python 3.6 or above, you can achieve this with a simple f-string .还假设您使用的是 Python 3.6 或更高版本,您可以使用简单的f-string来实现这一点。

print(f"https://.../{str(parameters[i])}site={site_codes[1]:04d}")

This will pad with leading zeros without the need to resort to zfill.这将填充前导零,而无需求助于 zfill。

Alternatively, or if you're running Python below 3.6, this will also work:或者,或者如果您在 3.6 以下运行 Python,这也将起作用:

print("https://.../{}site={:04d}".format(str(parameters[i]), site_codes[1]))

With a site code of 16, both of the above will give you站点代码为 16,以上两者都会给你

https://.../parametersite=0016 https://.../parametersite=0016

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

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