{"id":3079,"date":"2019-10-17T11:25:07","date_gmt":"2019-10-17T10:25:07","guid":{"rendered":"https:\/\/www.itersdesktop.com\/?p=3079"},"modified":"2020-05-29T09:04:24","modified_gmt":"2020-05-29T08:04:24","slug":"read-and-write-with-file-csv-in-python","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/vi\/2019\/10\/17\/read-and-write-with-file-csv-in-python\/","title":{"rendered":"\u0110\u1ecdc v\u00e0 vi\u1ebft d\u1eef li\u1ec7u v\u1edbi file CSV trong Python"},"content":{"rendered":"<p>[dropcap]N[\/dropcap]hi\u1ec1u ph\u1ea7n m\u1ec1m b\u1ea3ng t\u00ednh c\u00f3 th\u1ec3 m\u1edf \u0111\u01b0\u1ee3c \u0111\u1ecbnh d\u1ea1ng d\u1eef li\u1ec7u <em>csv<\/em>. Trong qu\u00e1 tr\u00ecnh \u0111\u01b0a k\u1ebft qu\u1ea3 ra m\u00e0n h\u00ecnh, nhi\u1ec1u gi\u00e1 tr\u1ecb mu\u1ed1n \u0111\u01b0\u1ee3c gi\u1eef l\u1ea1i v\u00e0 l\u01b0u trong file \u0111\u1ec3 ti\u1ec7n s\u1eed d\u1ee5ng v\u1ec1 sau. C\u00e1ch thu\u1eadn ti\u1ec7n nh\u1ea5t m\u00e0 nhi\u1ec1u ng\u01b0\u1eddi v\u1eabn d\u00f9ng l\u00e0 xu\u1ea5t d\u1eef li\u1ec7u ra d\u1ea1ng <em>csv<\/em>. T\u1ea1i sao ch\u1ecdn kh\u00f4ng ch\u1ecdn nh\u1eefng \u0111\u1ecbnh d\u1ea1ng kh\u00e1c nh\u01b0 <em>txt, dat, xls<\/em>, etc? T\u00ecm hi\u1ec3u th\u00eam v\u1ec1 c\u00f4ng d\u1ee5ng c\u1ee7a \u0111\u1ecbnh d\u1ea1ng csv. S\u1eed d\u1ee5ng Python t\u1eeb phi\u00ean b\u1ea3n 2.4 tr\u1edf \u0111i c\u00f3 th\u1ec3 \u0111\u1ecdc v\u00e0 vi\u1ebft v\u1edbi file <em>csv (comma separated values)<\/em>. Gi\u1ed1ng nh\u01b0 h\u1ea7u h\u1ebft c\u00e1c ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh kh\u00e1c, Python cho ph\u00e9p b\u1ea1n th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c v\u1edbi t\u1eadp tin.&nbsp;<b>Writing a CSV file&nbsp;<\/b>v\u1edbi Python c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n b\u1eb1ng vi\u1ec7c s\u1eed d\u1ee5ng csv module v\u00e0 t\u1ea1o m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng <strong>write<\/strong> \u0111\u1ec3 s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c <strong>WriteRow<\/strong>.&nbsp;<b>Reading a CSV file<\/b>&nbsp;th\u1ef1c hi\u1ec7n t\u01b0\u01a1ng t\u1ef1 b\u1eb1ng vi\u1ec7c t\u1ea1o ra m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng <strong>reader<\/strong> v\u00e0 s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c <strong>print<\/strong> \u0111\u1ec3 \u0111\u1ecdc t\u1eadp tin \u0111\u00f3. V\u00ec c\u00e1c thao t\u00e1c v\u1edbi file c\u1ea7n hi\u1ec3u nh\u1eefng kh\u00e1i ni\u1ec7m n\u00e2ng cao n\u00ean \u0111\u00f2i h\u1ecfi ng\u01b0\u1eddi \u0111\u1ecdc c\u00f3 ki\u1ebfn th\u1ee9c l\u1eadp tr\u00ecnh v\u1edbi Python m\u1edbi \u0111\u1ecdc \u0111\u01b0\u1ee3c ph\u1ea7n <b>\u0111\u1ecdc v\u00e0 vi\u1ebft d\u1eef li\u1ec7u v\u1edbi file CSV<\/b>.<\/p>\n<div id=\"toc\">\n<ul>\n<li><a href=\"#writing-to-csv-file\">Vi\u1ebft d\u1eef li\u1ec7u ra CSV file<\/a><\/li>\n<li><a href=\"#reading-a-csv-file\">\u0110\u1ecdc d\u1eef li\u1ec7u t\u1eeb CSV file<\/a><\/li>\n<\/ul>\n<\/div>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a3cdf5a2bbc2\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a3cdf5a2bbc2\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.itersdesktop.com\/vi\/2019\/10\/17\/read-and-write-with-file-csv-in-python\/#yeu-cau-toi-thieu\" >Y\u00eau c\u1ea7u t\u1ed1i thi\u1ec3u<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.itersdesktop.com\/vi\/2019\/10\/17\/read-and-write-with-file-csv-in-python\/#viet-du-lieu-len-file-csv\" >Vi\u1ebft d\u1eef li\u1ec7u l\u00ean file CSV<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.itersdesktop.com\/vi\/2019\/10\/17\/read-and-write-with-file-csv-in-python\/#doc-du-lieu-tu-file-csv\" >\u0110\u1ecdc d\u1eef li\u1ec7u t\u1eeb file CSV<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.itersdesktop.com\/vi\/2019\/10\/17\/read-and-write-with-file-csv-in-python\/#cach-tuong-tu\" >C\u00e1ch t\u01b0\u01a1ng t\u1ef1<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"yeu-cau-toi-thieu\"><\/span><span style=\"color: #008080;\">Y\u00eau c\u1ea7u t\u1ed1i thi\u1ec3u<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Ki\u1ebfn th\u1ee9c l\u1eadp tr\u00ecnh Python t\u1eeb phi\u00ean b\u1ea3n 2.4 tr\u1edf \u0111i. B\u1ea1n c\u0169ng c\u1ea7n c\u00f3 ki\u1ebfn th\u1ee9c v\u1ec1 k\u0129 thu\u1eadt l\u1eadp tr\u00ecnh c\u01a1 b\u1ea3n. Hi\u1ec3u bi\u1ebft c\u00e1c thao t\u00e1c c\u01a1 b\u1ea3n v\u1edbi file.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"viet-du-lieu-len-file-csv\"><\/span><span style=\"color: #008080;\">Vi\u1ebft d\u1eef li\u1ec7u l\u00ean file CSV<\/span><a name=\"writing-to-csv-file\"><\/a><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>B\u1eaft \u0111\u1ea7u v\u1edbi vi\u1ec7c import v\u00e0o ch\u01b0\u01a1ng tr\u00ecnh CSV module:<\/p>\n<pre class=\"\">import csv<\/pre>\n<p>K\u1ebf \u0111\u1ebfn, \u0111\u1ecbnh ngh\u0129a m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng &#8220;writer&#8221; (\u0111\u1eb7t t\u00ean l\u00e0 c), d\u00f9ng \u0111\u1ec3 vi\u1ebft d\u1eef li\u1ec7u l\u00ean CSV file. \u0110\u1ed1i s\u1ed1 c\u1ee7a ph\u01b0\u01a1ng th\u1ee9c writer \u0111\u01b0\u1ee3c nh\u1eadn t\u1eeb h\u00e0m open ch\u1ec9 ra t\u00ean t\u1eadp tin CSV v\u00e0 t\u00f9y ch\u1ecdn m\u1edf t\u1eadp tin wb \u0111\u1ec3 ch\u1ec9 ra: w = write v\u00e0 b=binary.<\/p>\n<pre class=\"\">c = csv.writer(open(\"MYFILE.csv\", \"wb\"))<\/pre>\n<p>B\u00e2y gi\u1edd d\u00f9ng ph\u01b0\u01a1ng th\u1ee9c writerow \u0111\u1ec3 vi\u1ebft t\u1eebng d\u00f2ng d\u1eef li\u1ec7u. Ph\u01b0\u01a1ng th\u1ee9c writerow nh\u1eadn m\u1ed9t tham s\u1ed1: l\u00e0 m\u1ed9t danh s\u00e1ch, v\u00e0 m\u1ed7i ph\u1ea7n t\u1eed trong danh s\u00e1ch t\u01b0\u01a1ng \u1ee9ng v\u1edbi m\u1ed9t c\u1ed9t trong file CSV. V\u00ed d\u1ee5, ch\u00fang ta th\u1eed l\u00e0m m\u1ed9t cu\u1ed1n s\u1ed5 \u0111\u1ecba ch\u1ec9 nh\u01b0 sau:<\/p>\n<pre>c.writerow([\"Name\",\"Address\",\"Telephone\",\"Fax\",\"E-mail\",\"Others\"])<\/pre>\n<p>V\u00e0 b\u1eb1ng c\u00e1ch \u0111\u00f3 ch\u00fang ta s\u1ebd l\u01b0u t\u1ea5t c\u1ea3 c\u00e1c d\u00f2ng d\u1eef li\u1ec7u v\u00e0o file theo c\u00e1ch n\u00e0y.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"doc-du-lieu-tu-file-csv\"><\/span><span style=\"color: #008080;\">\u0110\u1ecdc d\u1eef li\u1ec7u t\u1eeb file CSV<\/span><a name=\"reading-a-csv-file\"><\/a><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Tr\u01b0\u1edbc h\u1ebft t\u1ea1o m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng reader (gi\u1ea3 s\u1eed ta \u0111\u1eb7t t\u00ean cho \u0111\u1ed1i t\u01b0\u1ee3ng n\u00e0y l\u00e0: cr)<\/p>\n<pre class=\"\">cr = csv.reader(open(\"MYFILE.csv\",\"rb\"))<\/pre>\n<p>N\u1ebfu file CSV l\u01b0u d\u01b0\u1edbi d\u1ea1ng text, c\u1ea7n thay \u0111\u1ed5i m\u1ed9t ch\u00fat v\u1ec1 ch\u1ebf \u0111\u1ed9 \u0111\u1ecdc file t\u1eeb rb sang rt.<\/p>\n<pre class=\"\">cr = csv.reader(open(\"MYFILE.csv\",\"rt\"))<\/pre>\n<p>M\u1ed9t \u0111i\u1ec1u c\u1ea7n l\u01b0u \u00fd, n\u1ebfu file CSV c\u00f3 d\u00f2ng header (ti\u00eau \u0111\u1ec1 c\u1ee7a c\u00e1c c\u1ed9t &#8211; h\u00e0ng \u0111\u1ea7u ti\u00ean) th\u00ec n\u00f3 c\u0169ng \u0111\u01b0\u1ee3c b\u1ecf qua (skip\/ignore).<\/p>\n<pre class=\"\">cr.next()<\/pre>\n<p>V\u00e0 k\u1ebf ti\u1ebfp ch\u00fang ta l\u1ea5y t\u1eebng d\u00f2ng d\u1eef li\u1ec7u (theo d\u1ea1ng danh s\u00e1ch c\u00e1c c\u1ed9t) theo m\u1eabu nh\u01b0 sau:<\/p>\n<pre class=\"\">for row in cr:    \n    print row<\/pre>\n<p>D\u0129 nhi\u00ean ch\u00fang ta c\u00f3 th\u1ec3 tr\u00edch ch\u00ednh x\u00e1c nh\u1eefng n\u1ed9i dung c\u1ea7n thi\u1ebft c\u1ee7a m\u1ed9t d\u00f2ng b\u1eb1ng c\u00e1ch d\u00f9ng ch\u1ec9 m\u1ee5c (index):<\/p>\n<pre class=\"\">for row in reader:    \n    print row[2], row[-2]<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"cach-tuong-tu\"><\/span>C\u00e1ch t\u01b0\u01a1ng t\u1ef1<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u0110\u1ec3 \u0111\u1ecdc m\u1ed9t file csv, ch\u00fang ta c\u00f3 th\u1ec3 l\u00e0m t\u01b0\u01a1ng t\u1ef1 v\u1edbi <a href=\"#\">c\u00e2u l\u1ec7nh with<\/a> nh\u01b0 sau:<\/p>\n<pre class=\"lang:python decode:true \" title=\"\u0110\u1ecdc file csv v\u00e0 b\u1ecf c\u00e1c d\u00f2ng m\u1ed9t dictionary cho ph\u00e9p c\u00e1c value c\u00f3 c\u00f9ng key\">import csv\ndata_need_updated = {}\ndatafile = \"\/path\/to\/yourdata.csv\"\nwith open(datafile,\"rt\") as csvfile:\n    cr = csv.reader(csvfile)\n    next(cr)\n    for row in cr:\n        data_need_updated.setdefault(row[0],{})[row[4]] = row<\/pre>\n<p>N\u00f3i th\u00eam trong \u0111o\u1ea1n code b\u00ean tr\u00ean, c\u00e2u l\u1ec7nh trong v\u00f2ng for cho ph\u00e9p ch\u00e8n d\u1eef li\u1ec7u v\u00e0o m\u1ed9t c\u1ea5u tr\u00fac d\u1eef li\u1ec7u ki\u1ec3u dictionary cho ph\u00e9p nhi\u1ec1u values c\u00f3 c\u00f9ng key.","protected":false},"excerpt":{"rendered":"<p>[dropcap]N[\/dropcap]hi\u1ec1u ph\u1ea7n m\u1ec1m b\u1ea3ng t\u00ednh c\u00f3 th\u1ec3 m\u1edf \u0111\u01b0\u1ee3c \u0111\u1ecbnh d\u1ea1ng d\u1eef li\u1ec7u csv. Trong qu\u00e1 tr\u00ecnh \u0111\u01b0a k\u1ebft qu\u1ea3 ra m\u00e0n h\u00ecnh, nhi\u1ec1u gi\u00e1 tr\u1ecb mu\u1ed1n \u0111\u01b0\u1ee3c gi\u1eef l\u1ea1i&hellip; <\/p>\n","protected":false},"author":2,"featured_media":3092,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[196,122],"tags":[756,683,123,487,486],"class_list":["post-3079","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-file-type","category-python","tag-create-a-file","tag-csv","tag-python-language","tag-reading-a-csv-file","tag-writing-a-csv-file"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/3079","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/comments?post=3079"}],"version-history":[{"count":13,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/3079\/revisions"}],"predecessor-version":[{"id":3098,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/3079\/revisions\/3098"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/media\/3092"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/media?parent=3079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/categories?post=3079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/tags?post=3079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}