我试着四处寻找如何将python中的数字格式化为这种特定格式。将数字格式化为货币格式很容易理解,但这样做并不容易。
<td>{{ "{:,.2f} NOK".format(acc['AccountBalance']) }}</td>
所以我只想将12345678901格式化为1234.56.78901。我想在我的烧瓶模板网站上使用这个。每个数字总是11位,并且应该格式化为4位周期2位周期5位周期。
EDIT:这是在Flask模板中使用的,用于从SQLite表中检索并放入列表中的数字。是accs[‘ID’]在上面完成了这个格式化。
accs = db.execute(
'SELECT a.ID, ownerID, AccountBalance, AccountName'
' FROM atbl_bank_accounts a JOIN atbl_system_users u ON a.ownerID = u.ID'
' WHERE a.ownerID = ?',
(g.user['ID'],)
).fetchall()
这再次被张贴在网站上的一个表格中,使用烧瓶模板如下:
<table class="table table-striped table-hover table-borderless mt-5">
<thead>
<tr>
<th class="col-2" scope="col">Account Number</th>
<th class="col-4" scope="col">Account Name</th>
<th class="col-4" scope="col">Amount left</th>
<th class="col-2" scope="col">Actions</th>
</tr>
</thead>
<tbody>
{% for acc in accs %}
<tr>
<th scope="row">{{ acc['ID'] }}</th>
<td>{{ acc['AccountName'] }}</td>
<!--<td>{{ acc['AccountBalance'] }}</td>-->
<td>{{ "{:,.2f} NOK".format(acc['AccountBalance']) }}</td>
<td>
<button type="button" class="btn btn-light" data-toggle="tooltip" data-placement="top" title="Edit name of account"><i class="fas fa-edit"></i></button>
<button type="button" class="btn btn-light" data-toggle="tooltip" data-placement="top" title="Transfer money from account"><i class="fas fa-file-invoice-dollar"></i></button>
<button type="button" class="btn btn-light" data-toggle="tooltip" data-placement="top" title="Delete account"><i class="fas fa-trash"></i></button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
因此,我需要一个直接在HTML或python脚本中工作的解决方案。抱歉不够清楚。
我确实搜索了很多,但如果我跳过了一篇关于这件事的文章,我很抱歉。谢谢你提前回答。
将整数转换为str,并将切片与join
一起使用。
In [22]: p = 12345678901
In [23]: q = str(p)
In [24]: '.'.join((q[:4],q[4:-5],q[-5:]))
Out[24]: '1234.56.78901'
或者你可以使用切片对象,这基本上是一样的,但你可以命名它们。
In [33]: first, middle, last = slice(0,4),slice(4,-5),slice(-5,None)
In [34]: '.'.join((q[first],q[middle],q[last]))
Out[34]: '1234.56.78901'
感谢NoNickAvailable和wwii提供的提示。由于我使用的是烧瓶模板和循环,我不能直接使用wwii的答案。在这篇stackoverflow文章的帮助下,我成功地制作了一个工作版本。
我从数据库中检索数据,如下所示:
accs = db.execute(
'SELECT a.ID, ownerID, AccountBalance, AccountName'
' FROM atbl_bank_accounts a JOIN atbl_system_users u ON a.ownerID = u.ID'
' WHERE a.ownerID = ?',
(g.user['ID'],)
).fetchall()
然后我不得不使用dict
:
IDrows = [dict(row) for row in accs]
然后,为了用我非常具体的格式格式化数字,我使用了wwii和NoNickAvailable的答案:
for i in IDrows:
i['ID'] = '.'.join((str(i['ID'])[:4],str(i['ID'])[4:-5],str(i['ID'])[-5:]))
这些数字都是单独格式化的,很容易插入我的表格:
<table class="table table-striped table-hover table-borderless mt-5">
<thead>
<tr>
<th class="col-2" scope="col">Account Number</th>
<th class="col-4" scope="col">Account Name</th>
<th class="col-4" scope="col">Amount left</th>
<th class="col-2" scope="col">Actions</th>
</tr>
</thead>
<tbody>
{% for acc in IDrows %}
<tr>
<th scope="row">{{ acc['ID'] }}</th>
<td>{{ acc['AccountName'] }}</td>
<!--<td>{{ acc['AccountBalance'] }}</td>-->
<td>{{ "{:,.2f} NOK".format(acc['AccountBalance']) }}</td>
<td>
<button type="button" class="btn btn-light" data-toggle="tooltip" data-placement="top" title="Edit name of account"><i class="fas fa-edit"></i></button>
<button type="button" class="btn btn-light" data-toggle="tooltip" data-placement="top" title="Transfer money from account"><i class="fas fa-file-invoice-dollar"></i></button>
<button type="button" class="btn btn-light" data-toggle="tooltip" data-placement="top" title="Delete account"><i class="fas fa-trash"></i></button>
</td>
</tr>
{% endfor %}
</tbody>
</table>