【问题标题】:Concatenating \t python gives irregular spacing连接 \t python 给出不规则间距
【发布时间】:2014-09-05 19:58:41
【问题描述】:
w.write(str(entries[0]) + "\t" + (str(entries[1])) + "\n")

这给出了输出

Zygophyseter    1
Zygorhiza       1
Zygospore       1
Zygote_(band)   1
Zygote_intrafallopian_transfer  1
Zygotes 1
Zygovisti       1

为什么名称和数字1之间的不同行有如此不规则的间距

【问题讨论】:

标签: python


【解决方案1】:

您误解了标签的含义。制表符表示:从当前位置移动到下一个制表位

这些制表位的位置取决于您的文本编辑器或终端。通常它们位于每 8 列,Stack Overflow 将它们标记到 第四 列:

>>> def show_tabs():
...     print('\t'.join(['v'] * 8))
...     for i in range(1, 33):
...         print(''.join(map(lambda j: str(j)[-1], range(1, i))), 'tab to here', sep='\t')
... 
>>> show_tabs()
v   v   v   v   v   v   v   v
    tab to here
1   tab to here
12  tab to here
123 tab to here
1234    tab to here
12345   tab to here
123456  tab to here
1234567 tab to here
12345678    tab to here
123456789   tab to here
1234567890  tab to here
12345678901 tab to here
123456789012    tab to here
1234567890123   tab to here
12345678901234  tab to here
123456789012345 tab to here
1234567890123456    tab to here
12345678901234567   tab to here
123456789012345678  tab to here
1234567890123456789 tab to here
12345678901234567890    tab to here
123456789012345678901   tab to here
1234567890123456789012  tab to here
12345678901234567890123 tab to here
123456789012345678901234    tab to here
1234567890123456789012345   tab to here
12345678901234567890123456  tab to here
123456789012345678901234567 tab to here
1234567890123456789012345678    tab to here
12345678901234567890123456789   tab to here
123456789012345678901234567890  tab to here
1234567890123456789012345678901 tab to here

在您自己的终端中运行上述代码将很快显示您的选项卡设置。

在您的文本中,有一行短于 8 个字符,因此下一个制表位位于从开头开始的 8 个字符处。一行超过 16 个字符,因此下一个制表位为 24 个字符。大多数行包含 9 到 14 个字符,因此下一个制表位在 16 个字符处。换句话说,您的输出与在列之间使用制表符完全一致

如果您想生成包含完美对齐的列的文本输出,请调整您的制表符以适应特定的制表位配置(根据第一列的长度使用更多或更少的制表符),或者仅使用空格,并且再次调整间距。 Python 可以帮助处理后者,请参阅 formatting with str.format(),您可以在给定宽度的空白处左对齐或右对齐文本。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 2015-05-27
  • 2015-02-09
  • 2021-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多