{"id":129,"date":"2025-08-17T23:13:56","date_gmt":"2025-08-17T23:13:56","guid":{"rendered":"https:\/\/learn.rantissi.my.id\/?p=129"},"modified":"2025-08-17T23:13:57","modified_gmt":"2025-08-17T23:13:57","slug":"menyimpan-dan-membaca-file-teks-csv","status":"publish","type":"post","link":"https:\/\/learn.rantissi.my.id\/index.php\/2025\/08\/17\/menyimpan-dan-membaca-file-teks-csv\/","title":{"rendered":"Menyimpan dan Membaca File (Teks &amp; CSV)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"a-menulis-ke-file-txt\">A. Menulis ke File&nbsp;<code>.txt<\/code><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"data.txt\", \"w\") as file:\n    file.write(\"Halo Dunia!\\n\")\n    file.write(\"Ini baris kedua.\\n\")\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Gunakan mode&nbsp;<code>\"w\"<\/code>&nbsp;(write) \u2192 menulis (dan menghapus isi lama)<br>Gunakan mode&nbsp;<code>\"a\"<\/code>&nbsp;(append) \u2192 menambahkan ke akhir file<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"b-membaca-dari-file-txt\">B. Membaca dari File&nbsp;<code>.txt<\/code><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"data.txt\", \"r\") as file:\n    isi = file.read()\n    print(isi)\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Bisa juga&nbsp;<code>readline()<\/code>&nbsp;(satu baris) atau&nbsp;<code>readlines()<\/code>&nbsp;(list per baris)<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-menyimpan-ke-csv-comma-separated-values\">C. Menyimpan ke CSV (Comma-Separated Values)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import csv\n\nmurid = &#91;\n    {\"nama\": \"Ran\", \"nilai\": 85},\n    {\"nama\": \"Ali\", \"nilai\": 90},\n    {\"nama\": \"Dina\", \"nilai\": 78}\n]\n\nwith open(\"murid.csv\", \"w\", newline='') as file:\n    writer = csv.DictWriter(file, fieldnames=&#91;\"nama\", \"nilai\"])\n    writer.writeheader()\n    writer.writerows(murid)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"d-membaca-dari-file-csv\">D. Membaca dari File CSV<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import csv\n\nwith open(\"murid.csv\", \"r\") as file:\n    reader = csv.DictReader(file)\n    for row in reader:\n        print(row&#91;\"nama\"], \"mendapat\", row&#91;\"nilai\"])<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A. Menulis ke File&nbsp;.txt Gunakan mode&nbsp;&#8220;w&#8221;&nbsp;(write) \u2192 menulis (dan menghapus isi lama)Gunakan mode&nbsp;&#8220;a&#8221;&nbsp;(append) \u2192 menambahkan ke akhir file B. Membaca dari File&nbsp;.txt Bisa juga&nbsp;readline()&nbsp;(satu baris) atau&nbsp;readlines()&nbsp;(list per baris) C. Menyimpan ke CSV (Comma-Separated Values) D. Membaca dari File CSV<\/p>\n","protected":false},"author":1,"featured_media":20,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7],"tags":[],"class_list":["post-129","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","category-python"],"_links":{"self":[{"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":1,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":130,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/posts\/129\/revisions\/130"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/media\/20"}],"wp:attachment":[{"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn.rantissi.my.id\/index.php\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}