2017-04-11 57 views
-6
with open('list.txt') as f: 
    print " ".join(line.strip() for line in f) 

Though the code seems to be correct, but facing syntax error !這段代碼的語法有什麼問題?

+3

如果這是PY3,必須用'print'作爲一個函數:'打印( )' – TemporalWolf

+0

嘗試'mystr =「」'然後'mystr.join(...)' –

+0

代碼在python2.x中正常工作,在Python3.x中使用print()作爲@TemporalWolf聲明。因此'print(「」.join(line.strip()for f))'' – Montmons

回答

0

您正在使用Python版本3,使用此

with open('line.txt') as f: 
    print(" ".join(line.strip() for line in f))