2
作爲一個初學者Python程序員,我希望這是一些愚蠢的錯誤(或缺乏理解),有一個簡單的解決方案:與...:爲...:在單行
/tmp> python3 -c 'with open("t","r") as f: for l in f: print(l)'
File "<string>", line 1
with open("t","r") as f: for l in f: print(l)
^
SyntaxError: invalid syntax
我可以在ONELINE嵌入一個換行符,而是試圖理解爲什麼上面不起作用
/tmp> python3 -c $'with open("t","r") as f:\n for l in f: print(l)'
Hi there
爲什麼不'print'\ n'.join(l for l in open(「t」))'? –
在同一行中不能有兩個*語句*。 –
或http://stackoverflow.com/questions/2043453/executing-python-multi-line-statements-in-the-one-line-command-line –