我想使用以下内容将一些预定义的文本写入文件:
text="this is line one\n
this is line two\n
this is line three"
echo -e $text > filename
Run Code Online (Sandbox Code Playgroud)
我期待这样的事情:
this is line one
this is line two
this is line three
Run Code Online (Sandbox Code Playgroud)
但得到了这个:
this is line one
this is line two
this is line three
Run Code Online (Sandbox Code Playgroud)
我很肯定每个人都没有空间\n,但额外的空间是如何产生的?
我不确定在什么标题下完全考虑这个问题,编码高尔夫看起来是合适的,如果有点不明确.
我对python有一点了解,但看起来很难"阅读".我看到它的方式,理解可能会完成与以下代码相同:
for i in range(10): if i == 9: print('i equals 9')
Run Code Online (Sandbox Code Playgroud)
这段代码比理解目前的工作方式更容易阅读,但我注意到你不能在一行中有两个':'......这也带给我了...
有什么方法可以让以下示例进入ONE LINE.
try:
if sam[0] != 'harry':
print('hello', sam)
except:
pass
Run Code Online (Sandbox Code Playgroud)
像这样的东西会很棒:
try: if sam[0] != 'harry': print('hellp', sam)
except:pass
Run Code Online (Sandbox Code Playgroud)
但是我又遇到了相互冲突的':'我也很想知道是否有办法运行尝试(或类似的东西),除非,我需要放置除了之外似乎完全没有意义:传入那里.这是一条浪费的线.
谢谢你输入......这里有一个笑脸:D
如何在 python REPL 中编写多行代码?:
aircraftdeMacBook-Pro:~ ldl$ python
Python 2.7.10 (default, Jul 30 2016, 19:40:32)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Run Code Online (Sandbox Code Playgroud)
例如示例:
i = 0
while i < 10:
i += 1
print i
Run Code Online (Sandbox Code Playgroud)
在终端中,我不知道在 python shell 中热换行:
我测试了Control+ Enter、Shift+Enter和Command+ Enter,它们都错了:
>>> while i < 10:
... print i
File "<stdin>", line 2
print i
^
IndentationError: expected …Run Code Online (Sandbox Code Playgroud)