2015-12-29 56 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