如何使用 python 在 data/String 中添加 space?
How to add space in data/String using python?
我的数据库中有一些数据。它看起来像 84819010,我想把它变成 8481 90 10
我使用以下代码并收到以下错误:
information_table = data.cur.execute("""
SELECT p.gs_com_code, p.gs_code FROM product p
INNER JOIN (
SELECT pp.p_gs_com_code From product_purchase pp
) t ON t.p_gs_com_code = p.gs_com_code
""",).fetchall()
for info in information_table:
t = info[1]
sep = "{}{}{}{} {}{} {}{}"
gs_code = sep.format(*t)
print(gs_code)
错误:
gs_code = sep.format(*t)
IndexError: Replacement index 7 out of range for positional args tuple
我的信息看起来像 84819010,我想把它变成 8481 90 10
使用f-strings
:
for info in information_table:
gs_code = f"{info[:4]} {info[4:6]} {info[6:]}"
print(gs_code)
# Output
6804 22 00
7018 10 00
8712 00 00
6804 22 00
我的数据库中有一些数据。它看起来像 84819010,我想把它变成 8481 90 10 我使用以下代码并收到以下错误:
information_table = data.cur.execute("""
SELECT p.gs_com_code, p.gs_code FROM product p
INNER JOIN (
SELECT pp.p_gs_com_code From product_purchase pp
) t ON t.p_gs_com_code = p.gs_com_code
""",).fetchall()
for info in information_table:
t = info[1]
sep = "{}{}{}{} {}{} {}{}"
gs_code = sep.format(*t)
print(gs_code)
错误:
gs_code = sep.format(*t)
IndexError: Replacement index 7 out of range for positional args tuple
我的信息看起来像 84819010,我想把它变成 8481 90 10
使用f-strings
:
for info in information_table:
gs_code = f"{info[:4]} {info[4:6]} {info[6:]}"
print(gs_code)
# Output
6804 22 00
7018 10 00
8712 00 00
6804 22 00