-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 !這段代碼的語法有什麼問題?
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 !這段代碼的語法有什麼問題?
在Python 3,打印是一個函數。在這裏看到:https://docs.python.org/3/whatsnew/3.0.html#print-is-a-function
嘗試:
with open('list.txt') as f:
print(" ".join(line.strip() for line in f))
您正在使用Python版本3,使用此
with open('line.txt') as f:
print(" ".join(line.strip() for line in f))
如果這是PY3,必須用'print'作爲一個函數:'打印( )' – TemporalWolf
嘗試'mystr =「」'然後'mystr.join(...)' –
代碼在python2.x中正常工作,在Python3.x中使用print()作爲@TemporalWolf聲明。因此'print(「」.join(line.strip()for f))'' – Montmons