{"id":1046,"date":"2023-09-11T05:22:45","date_gmt":"2023-09-11T05:22:45","guid":{"rendered":"https:\/\/snersbots.co.uk\/?p=1046"},"modified":"2023-09-11T05:26:54","modified_gmt":"2023-09-11T05:26:54","slug":"photo-consolidation-project-write-to-file","status":"publish","type":"post","link":"https:\/\/snersbots.co.uk\/index.php\/photo-consolidation-project-write-to-file\/","title":{"rendered":"Photo Consolidation Project &#8211; Write to File"},"content":{"rendered":"\n<p>In order to get working software sooner, I&#8217;ve decided to write the data to a csv file. This will allow us to get cracking on the photos without having to wait for the completed project. And for some people, a csv file will be enough. The database will just be extra complexity. But there is plenty of time to explore that route. For now, I just need to be able to write something to a csv file.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Write to csv File<\/h2>\n\n\n\n<p>ChatGPT gave me this code:<\/p>\n\n\n\n<pre class=\"wp-block-code has-background\" style=\"background-color:#e9f1f7\"><code>import csv\n\ncsv_filepath = \"C:\\\\Users\\\\*****\\\\Desktop\\\\Team Turkey Run 2019\\\\TestOutput.csv\"\n\nwith open(csv_filepath, 'w', newline='') as csvfile:\n    fieldnames = &#91;'File Name', 'File Path', 'MD5 Hash']\n    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)\n    writer.writeheader()\n    writer.writerow({'File Name': 'photo.jpg', 'File Path': 'file_path', 'MD5 Hash': 'md5_hash'})\n<div style=\"color: #000000;background-color: #ffffff;font-family: Consolas, 'Courier New', monospace;font-weight: normal;font-size: 14px;line-height: 19px;white-space: pre;\"><div><span style=\"color: #000000;\">&nbsp; &nbsp; &nbsp;<\/span><\/div><\/div><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Code Walkthrough<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <a href=\"https:\/\/docs.python.org\/3\/library\/csv.html#module-csv\">csv module<\/a> implements classes to read and write tabular data in CSV format.<\/li>\n\n\n\n<li>This fie is opened in write mode -&#8216;w&#8217;<\/li>\n\n\n\n<li>The <a href=\"https:\/\/docs.python.org\/3\/library\/functions.html#open\">open <\/a>command will attempt to translate any newline characters into the appropriate character for the Operating System.<\/li>\n\n\n\n<li>When using the <a href=\"https:\/\/docs.python.org\/3\/library\/csv.html\">csv module<\/a>, it is recommended to specify <code>newline = ''<\/code>. <\/li>\n\n\n\n<li><code>as csvfile<\/code> tells the code that the <a href=\"https:\/\/docs.python.org\/3\/library\/csv.html\">csv module<\/a> will be handling the writing to file.<\/li>\n\n\n\n<li>When writing a csv, it is required to tell it the <code>fieldnames<\/code>. For reading a file, this line is optional.<\/li>\n\n\n\n<li>the <a href=\"https:\/\/docs.python.org\/3\/library\/csv.html#csv.DictWriter\">csv.DictWriter<\/a> operates like a regular writer but maps dictionaries onto output rows.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is a csv File?<\/h2>\n\n\n\n<p>It is a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Comma-separated_values\">Comma Separated Values<\/a> files. This is very useful to us, because we can write to it as though it was just another text file. And yet the file can be opened in any spreadsheet application. In my case I use Libre Office Calc. But it works just the same in Excel or any other spreadsheet tool.<\/p>\n\n\n\n<p>If you get a &#8216;Text Import&#8217; screen when you open the file, just press OK. It will sort itself out.<\/p>\n\n\n\n<p>If you make changes, you will need to &#8216;save as&#8217; and change the file type to the default file type for your spreadsheet application. The csv format does not keep any formatting information. It only keeps text data.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In order to get working software sooner, I&#8217;ve decided to write the data to a csv file. This will allow us to get cracking on the photos without having to wait for the completed project. And for some people, a csv file will be enough. The database will just be extra complexity. But there is <a class=\"read-more\" href=\"https:\/\/snersbots.co.uk\/index.php\/photo-consolidation-project-write-to-file\/\">The full story here&#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":1055,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70,8],"tags":[69,71],"class_list":["post-1046","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-desktop-apps","category-software","tag-duplicate-photos","tag-python"],"_links":{"self":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/1046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/comments?post=1046"}],"version-history":[{"count":10,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/1046\/revisions"}],"predecessor-version":[{"id":1060,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/1046\/revisions\/1060"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/media\/1055"}],"wp:attachment":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/media?parent=1046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/categories?post=1046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/tags?post=1046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}