2015-12-29 96 views
1

我對一段時間後寫的代碼感到困惑(yep沒有評論gotcha!),這是with open"at"標誌。它是否存在是因爲我找不到任何東西,如果不是,python(我使用3.4)只是忽略't'部分。打開「at」標誌我搞砸了嗎?

with open(cumulative_file ,'at') as c: 
    c_csv = csv.writer(c) 
    c_csv.writerows(hv_today) 
+0

Python區分二進制和文本I/O。以二進制模式打開的文件(包括mode參數中的'b')將內容作爲字節對象返回,而不進行任何解碼。在文本模式下(默認情況下,或當't'包含在mode參數中時),文件內容以str形式返回,字節首先使用平臺相關編碼進行解碼,或者使用指定的編碼(如果給出)。閱讀https://docs.python.org/3/library/functions.html#open – Kasramvd

+0

對不起,我沒有看到這在我的搜索謝謝 – theakson

+0

沒問題,花了我一會兒也找到它;)你問題仍然很好,所以人們可以直接在'at'標誌上找到問題:) – poke

回答

1

是的,它是有效的。檢查的open()功能的文檔:

可用的模式有:

Character  Meaning 
'r' open for reading (default) 
'w' open for writing, truncating the file first 
'x' open for exclusive creation, failing if the file already exists 
'a' open for writing, appending to the end of the file if it exists 
'b' binary mode 
't' text mode (default) 
'+' open a disk file for updating (reading and writing) 
'U' universal newlines mode (deprecated) 

在文本模式(默認,或當't'包括在mode 參數),該文件的內容被返回爲str,字節 已經首先使用平臺相關編碼進行解碼,或者如果給定,則使用 指定的編碼。

+0

我完全失去了它某種原因,謝謝你不給我一個難的時間,並給出這樣一個完整的答案 – theakson

0

't'用於文本模式。在某些操作系統上,它在讀取或寫入時有所不同。

+0

這是拙劣的餡餅時間。我唯一的防守就是我是個白癡。感謝您的澄清 – theakson

0

不,'t'表示文本模式。你可以省略這個,因爲文本模式是打開文件的默認模式。

Python docs Open

+0

我覺得很小:-)謝謝不要傻笑 – theakson