[英]Decoding a text file in python
What would the syntax look like when decoding a file,when I try to open the file it shows this #<_io.TextIOWrapper name='DOB.txt' mode='r+' encoding='cp1252'>解码文件时语法会是什么样子,当我尝试打开文件时,它会显示 #<_io.TextIOWrapper name='DOB.txt' mode='r+' encoding='cp1252'>
Your resulting printout is actually your opened file (it's an object).您生成的打印输出实际上是您打开的文件(它是一个对象)。
I suppose you're opening the file like this:我想你是这样打开文件的:
f = open("DOB.txt", "r+", encoding="cp1252")
Directly printing f
will result in your current printed string.直接打印f
将导致您当前打印的字符串。
To actually retrieve the contents of your file, you need to call the read
method.要实际检索文件的内容,您需要调用read
方法。
Example:例子:
f = open("DOB.txt", "r+", encoding="cp1252")
content = f.read()
content
will become the contents of your file as a single string whereas newlines are being encoded by a newline character (depending on your OS either \r\n
(on Windows) or \n
on a UNIX-based OS) content
将成为您文件的内容作为单个字符串,而换行符由换行符编码(取决于您的操作系统\r\n
(在 Windows 上)或\n
在基于 UNIX 的操作系统上)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.