{"id":364,"date":"2014-07-03T13:07:30","date_gmt":"2014-07-03T13:07:30","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=364"},"modified":"2014-07-03T13:07:30","modified_gmt":"2014-07-03T13:07:30","slug":"python%e6%96%87%e4%bb%b6%e8%af%bb%e5%8f%96%e7%9a%843%e4%b8%ad%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/364.html","title":{"rendered":"Python\u6587\u4ef6\u64cd\u4f5c\u5c0f\u7ed3"},"content":{"rendered":"<pre class=\"lang:python decode:true\">#!\/usr\/bin\/env python\n\n#####\nfp=open(\"ip.txt\", \"r\")\nfile_lines=fp.readlines()\nfor each_line in  file_lines:\n\tprint each_line,\nfp.close()\n#####\nfile_object=open(\"ip.txt\", \"r\")\nfile_content=file_object.read()\nprint file_content,\nfile_object.close()\n#####\nfile_object=open(\"ip.txt\", \"r\")\nwhile True:\n\tfile_line=file_object.readline()\n\tif file_line:\n\t\tprint file_line,\n\telse:\n\t\tbreak\nfile_object.close()\n#####<\/pre>\n<h6>\u8bf4\u660e\u5982\u4e0b\uff1a<span style=\"color: #ff0000;\">python\u4e2dread() readline()\u4ee5\u53careadlines()\u533a\u522b<\/span><\/h6>\n<ul>\n<li>read() \u6bcf\u6b21\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\uff0c\u5b83\u901a\u5e38\u5c06\u8bfb\u53d6\u5230\u5e95\u6587\u4ef6\u5185\u5bb9\u653e\u5230\u4e00\u4e2a\u5b57\u7b26\u4e32\u53d8\u91cf\u4e2d\uff0c\u4e5f\u5c31\u662f\u8bf4 fp.read() \u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1b<\/li>\n<li>readline()\u6bcf\u6b21\u53ea\u8bfb\u53d6\u6587\u4ef6\u7684\u4e00\u884c\uff0c\u901a\u5e38\u4e5f\u662f\u8bfb\u53d6\u5230\u7684\u4e00\u884c\u5185\u5bb9\u653e\u5230\u4e00\u4e2a\u5b57\u7b26\u4e32\u53d8\u91cf\u4e2d\uff0c\u8fd4\u56destr\u7c7b\u578b\uff1b<\/li>\n<li>readlines()\u6bcf\u6b21\u6309\u884c\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5185\u5bb9\uff0c\u5c06\u8bfb\u53d6\u5230\u7684\u5185\u5bb9\u653e\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u8fd4\u56delist\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<hr \/>\n<p>&nbsp;<\/p>\n<p>Update@2014\u5e7412\u670822\u65e5 20:49:48<\/p>\n<p>\u6700\u8fd1\u7ecf\u5e38\u4f1a\u9700\u8981\u7528\u5230Python\u8bfb\u5199\u6587\u4ef6\uff08\u5c06\u4ee3\u7801\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u6253\u5370\u5185\u5bb9\/\u7ed3\u679c\u5199\u5165\u6587\u4ef6\uff09\uff0c\u4f46\u53d1\u73b0\u4e0d\u600e\u4e48\u719f\u7ec3\uff0c\u4e3a\u4e86\u907f\u514d\u8fd9\u4e00\u95ee\u9898\uff0c\u73b0\u5728\u5728\u539f\u6587\u7684\u57fa\u7840\u4e0a\u518d\u6b21\u603b\u7ed3\u4e00\u4e0bPython\u8bfb\u5199\u6587\u4ef6\u7684\u65b9\u6cd5\uff1a<\/p>\n<h5>\u6b65\u9aa41.\u5148\u4f7f\u7528open()\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61<\/h5>\n<p><a href=\"https:\/\/docs.python.org\/2\/library\/functions.html#open\" target=\"_blank\">https:\/\/docs.python.org\/2\/library\/functions.html#open<\/a><\/p>\n<pre class=\"lang:default decode:true\">open(name[, mode[, buffering]])\nOpen a file, returning an object of the file type described in section File Objects. If the file cannot be opened, IOError is raised. When opening a file, it\u2019s preferable to use open() instead of invoking the file constructor directly.<\/pre>\n<p><strong><span style=\"color: #ff0000;\">\u63d0\u793a\uff1aopen()\u51fd\u6570\u6210\u529f\u6267\u884c\u540e\u8fd4\u56de\u7684\u6587\u4ef6\u5bf9\u8c61\u662f\u53ef\u8fed\u4ee3\u7684\uff08iterable\uff09\u3002<\/span><\/strong><\/p>\n<h5>\u6b65\u9aa42.\u6587\u4ef6\u8bfb\u53d6<\/h5>\n<p><a href=\"https:\/\/docs.python.org\/2\/tutorial\/inputoutput.html#methods-of-file-objects\" target=\"_blank\">https:\/\/docs.python.org\/2\/tutorial\/inputoutput.html#methods-of-file-objects<\/a><\/p>\n<h6>\u4e00\u4e2a\u6587\u4ef6\u8bfb\u53d6\/\u5199\u5165\u7684\u4f8b\u5b50\uff1a<\/h6>\n<pre class=\"lang:default decode:true\">import zlib\nfp = open('aaa', 'rb')\ncon_0 = fp.read()\nfp.close()\n\nprint len(con_0)\ndecompressed_data = zlib.decompress(con_0, 16+zlib.MAX_WBITS)\nprint len(decompressed_data)\n\nfp = open('bbb', 'wb')\nfp.write(decompressed_data)\nfp.close()<\/pre>\n<h5>\u6b65\u9aa43.\u6587\u4ef6\u5199\u5165<\/h5>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/12377473\/python-write-versus-writelines-and-concatenated-strings\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/12377473\/python-write-versus-writelines-and-concatenated-strings<\/a><\/p>\n<p>write()\u548cwritelines()\u65b9\u6cd5\u7684\u5f02\u540c\uff1a<\/p>\n<p>writelines() expects a list of strings[or any iterable object producing strings], while write() expects a single string.\uff08<span style=\"color: #ff0000;\"><strong>write()\u662f\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\uff1bwritelines()\u662f\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\/\u53ef\u8fed\u4ee3\u5bf9\u8c61\u5199\u5165\u6587\u4ef6<\/strong><\/span>\uff09<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<h4>\u5982\u4f55\u5c06Unicode\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\uff1f<\/h4>\n<p><a href=\"http:\/\/blog.csdn.net\/liukeforever\/article\/details\/6230729\" target=\"_blank\">python how to write unicode to a txt file<\/a><\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/6048085\/python-write-unicode-text-to-a-text-file\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/6048085\/python-write-unicode-text-to-a-text-file<\/a><\/p>\n<pre class=\"lang:default decode:true\">&gt;&gt; txt = u\"\u00e4hnlicher als gew\u00f6hnlich \u00fcblich\"\n&gt;&gt; import codecs\n&gt;&gt; codecs.open(\"tmp.txt\", \"wb\", \"utf8\").write(txt)\n&gt;&gt; codecs.open(\"tmp.txt\", \"rb\", \"utf8\").read()<\/pre>\n<pre class=\"lang:default decode:true \">In [3]: codecs.open?\nType:        function\nString form: &lt;function open at 0x02957430&gt;\nFile:        c:python27libcodecs.py\nDefinition:  codecs.open(filename, mode='rb', encoding=None, errors='strict', buffering=1)\nDocstring:\nOpen an encoded file using the given mode and return a wrapped version providing transparent encoding\/decoding.\n\nNote: The wrapped version will only accept the object format defined by the codecs, i.e. Unicode objects for most builtin codecs. Output is also codec dependent and will usually be Unicode as well.\n\nFiles are always opened in binary mode, even if no binary mode was specified. This is done to avoid data loss due to encodings using 8-bit values. The default file mode is 'rb' meaning to open the file in binary read mode.\n\nencoding specifies the encoding which is to be used for the file.\n\nerrors may be given to define the error handling. It defaults to 'strict' which causes ValueErrors to be raised in case an encoding error occurs.\n\nbuffering has the same meaning as for the builtin open() API. It defaults to line buffered.\n\nThe returned wrapped file object provides an extra attribute. encoding which allows querying the used encoding. This attribute is only available if an encoding was specified as parameter.<\/pre>\n<p>\u5373\uff0c\u4f7f\u7528codecs\u6a21\u5757\u5728\u6253\u5f00\u6587\u4ef6\u7684\u65f6\u5019\u6307\u5b9a\u7f16\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>#!\/usr\/bin\/env python ##### fp=open(&#8220;ip.txt&#8221;, &#8220;r&#8221;) file [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[177,8],"class_list":["post-364","post","type-post","status-publish","format-standard","hentry","category-programing","tag-codecs","tag-python"],"views":3124,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/364","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=364"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/364\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}