Python:在不使用.zfill的情况下在一位数字中添加零

时间:2019-10-11 23:54:12

标签: micropython

我当前使用的是micropython,它没有.zfill方法。 我试图获得的是获取UTC的YYMMDDhhmmss。 例如,它给我的时间是

t = (2019, 10, 11, 3, 40, 8, 686538, None)

我可以使用t [:6]访问所需的内容。现在问题出在一个数字3和8上。我能够显示1910113408,但我需要得到19101 03 40 08 需要在那些2之前得到零。我用

t = "".join(map(str,t))
t = t[2:]

所以我的想法是遍历 t ,然后检查该数字是否小于10。我将在其前面添加零,以替换数字。这就是我想出的。

t = (2019, 1, 1, 2, 40, 0)
t = list(t)

for i in t:
    if t[i] < 10:
        t[i] = 0+t[i]
    t[i] = t[i] 
print(t)

但是,这给了我 IndexError:列表索引超出范围 请帮忙,我是编码/ python的新手。

2 个答案:

答案 0 :(得分:1)

我建议您使用Python的字符串格式语法。

>> t = (2019, 10, 11, 3, 40, 8, 686538, None)
>> r = ("%d%02d%02d%02d%02d%02d" % t[:-2])[2:]
>> print(r)
191011034008

让我们看看这里发生了什么:

  • %d表示“显示数字”
  • %2d的意思是“显示至少2位数字”
  • %02d的意思是“显示一个数字,至少2位数字,用零填充”

因此,我们正在输入所有相关数字,根据需要填充它们,然后从“ 2019”中减去“ 20”。

答案 1 :(得分:1)

使用时

I/flutter (26322): zelda
I/flutter (26322): {
I/flutter (26322):   "status": 200,
I/flutter (26322):   "error": null,
I/flutter (26322):   "data": [
I/flutter (26322):     {
I/flutter (26322):       "id": 1025,
I/flutter (26322):       "cover": 81860,
I/flutter (26322):       "name": "Zelda II: The Adventure of Link",
I/flutter (26322):       "screenshots": [
I/flutter (26322):         19444,
I/flutter (26322):         179229,
I/flutter (26322):         179230,
I/flutter (26322):         179231,
I/flutter (26322):         179232,
I/flutter (26322):         179233,
I/flutter (26322):         179234,
I/flutter (26322):         179235,
I/flutter (26322):         179236
I/flutter (26322):       ]
I/flutter (26322):     },


import 'package:igdb_client/igdb_client.dart';

class ApiHelper {

  static const String API_KEY = 'xxxxxxx';

  static void apiSearch(String text) async {

    var client = new IGDBClient("xxxx", API_KEY);

    var gamesResponse = await client.games(new IGDBRequestParameters(
      search: text,
      fields: [ "id", "name", "storyline", "screenshots", "cover" ],

    ));


    if (gamesResponse.isSuccess()) {

      printResponse(gamesResponse);


    } else {
      print("ERROR");
    }

  }

  static printResponse(IGDBResponse resp) {
    print(IGDBHelpers.getPrettyStringFromMap(resp.toMap()));
  }

}

class Game {

  String id;
  String name;
  String cover;
  String storyline;
  List<String> screenshots;

}

for i in t: 不是索引,每个项目。

i

如果要使用索引,请执行以下操作:

>>> for i in t:
...     print(i)
...
2019
10
11
3
40
8
686538
None

创建“ 191011034008”的另一种方法

>>> for i, v in enumerate(t):
...     print("{} is {}".format(i,v))
...
0 is 2019
1 is 10
2 is 11
3 is 3
4 is 40
5 is 8
6 is 686538
7 is None

请注意:

  1. >>> t = (2019, 10, 11, 3, 40, 8, 686538, None) >>> "".join(map(lambda x: "%02d" % x, t[:6])) '20191011034008' >>> "".join(map(lambda x: "%02d" % x, t[:6]))[2:] '191011034008' 在参数小于10时添加前导零,否则(大于或等于10)使用其自身。所以year仍然是4位数的字符串。

  2. 此lambda并不希望参数为None。

我在https://micropython.org/unicorn/

上测试了此代码

编辑:

str.format方法版本:

%02d

"".join(map(lambda x: "{:02d}".format(x), t[:6]))[2:]

第二个示例的"".join(map(lambda x: "{0:02d}".format(x), t[:6]))[2:] 是参数索引。 如果要指定参数索引,可以使用参数索引(例如:格式字符串和参数之间的位置不匹配,要多次写入相同的参数...等等)。

0