简体   繁体   English

如何用\\ t符号制作表格?

[英]How do I Make A Table With \t symbol?

Right now, I'm working on "How to Think Like A Computer Scientist". 现在,我正在研究“如何像计算机科学家一样思考”。 I'm on Simple Tables . 我在简单桌子上 This lesson is making simple tables. 本课正在制作简单的表格。 The first example runs fine inside the browser, but when I take it Sublime, the \\t symbol prints. 第一个示例在浏览器中运行良好,但是当我使用Sublime时,会打印\\ t符号。 It does not do this in the browser. 它不会在浏览器中执行此操作。 I even copied the code right into sublime. 我什至将代码直接复制了下来。

Here's my code: 这是我的代码:

print("n", '\t', "2**n")
print("---", '\t', "-----")

for x in range(13):
    print(x, '\t', 2 ** x)

and my result is: 我的结果是:

('n', '\t', '2**n')
('---', '\t', '-----')
(0, '\t', 1)
(1, '\t', 2)
(2, '\t', 4)
(3, '\t', 8)
(4, '\t', 16)
(5, '\t', 32)
(6, '\t', 64)
(7, '\t', 128)
(8, '\t', 256)
(9, '\t', 512)
(10, '\t', 1024)
(11, '\t', 2048)
(12, '\t', 4096)

How do I get the parenthesis and the \\t symbol to go away. 如何获得括号和\\ t符号以消失。 I know that the parenthesis shouldn't be there, so I'm confused with that and I have no idea how to get rid of the \\t symbol without getting the "unexpected character" error. 我知道括号不应该在那儿,所以我对此感到困惑,而且我不知道如何在不出现“意外字符”错误的情况下摆脱\\ t符号。

I think you're using Python 2 and printing tuples. 我认为您正在使用Python 2并打印元组。 At the top of your file put from __future__ import print_function and it will look the way you expect. from __future__ import print_function放入文件的顶部, from __future__ import print_function ,它将看起来像您期望的那样。 Better yet, upgrade to Python 3! 更好的是,升级到Python 3! There are installers available for Mac* and PC from the Python website, and most (maybe all?) Linux distributions have Python 3 in the default package manager's repositories. Python网站上有适用于Mac *和PC的安装程序,并且大多数(也许是所有?)Linux发行版在默认程序包管理器的存储库中都具有Python 3。

To explain in a little more detail what's going on, check out one of the Python core developer's answers to another question . 要更详细地说明发生了什么,请查看Python核心开发人员对另一个问题的解答之一

Since you're running Python 2, where print is a statement , the statement only sees one argument : the tuple (x, '\\t', 2 ** x) . 由于您正在运行Python 2,其中print是一条语句 ,因此该语句仅包含一个参数 :元组(x, '\\t', 2 ** x) print tries to call __str__ on all the objects it has been given, but since it sees only one object in this case, the tuple, it only tries to call __str__ on that. print尝试在给__str__的所有对象上调用__str__ ,但是由于在这种情况下它仅看到一个对象(元组),因此它仅尝试在其上调用__str__ Since tuple does not define __str__ , __repr__ gets called instead. 由于tuple未定义__str____repr__会调用__repr__ You can see in the source that tuples call repr() on all the items in a tuple. 您可以在源代码中看到元组对元组中的所有项目调用repr() The repr of the string '\\t' is '\\t' . 字符串'\\t'的代表是'\\t' (This gets into the difference between repr and str in Python a bit). (这有点影响了Python中reprstr之间的区别)。 Whenever you use from __future__ import print_function , the function will see three separate arguments , and try to call __str__ on each of them. 每当您from __future__ import print_function ,该函数都会看到三个单独的参数 ,并尝试在每个参数上调用__str__ This will print out the way that you want. 这将打印出所需的方式。

For this specific case, you'll want to make sure spaces do not get inserted between arguments, so put in the keyword argument sep='' : 对于这种特定情况,您将要确保在参数之间不插入空格,因此请在关键字参数sep=''

print(x, '\t', 2**, sep='' )

* Note: On Mac OS X it's usually recommended to use Homebrew: http://docs.python-guide.org/en/latest/starting/install3/osx/ * 注意:在Mac OS X上,通常建议使用Homebrew: http : //docs.python-guide.org/en/latest/starting/install3/osx/

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

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