1
我試圖打開2個文件,其中一個有內容,一個是空的。對於不是標題的行,我在將處理過的行寫入空文件之前先讀取每行並處理它,直到到達第一個文件的末尾。Python文件I/O:無效的語法?
我在第二行收到'無效語法錯誤(打開...),不知道爲什麼。
try:
with open(file_read, 'r') as file_r, open(file_write, 'w') as file_w:
for line in file_r:
while line != '':
if count > 10:
line = line.split()
colour_int = int(line[-1]) # colour is stored as the last (4th) value in each line
red = (colour_int >> 16) & 255
green = (colour_int >> 8) & 255
blue = colour_int & 255
new_line = str.join([ line[0], line[1], line[2], red, green, blue ])
file_w.write(new_line + '\n')
count += 1
except IOError as e:
print 'Operation failed: %s' % e.strerror
什麼版本的Python? 2.7? – rdodev
是的,Python 2.7 – bard
您是否嘗試過下面的解決方案? – rdodev