<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/kindle-clippings-txt-with-python/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://maxhumber.com/kindle-clippings-python"> Max Humber</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Exactly a year ago I posted <a rel="nofollow" href="http://maxhumber.com/kindle-clippings-r" target="_blank">Kindle clippings.txt with R</a>. Since then things have changed… I’m a Pythonista now! Consequently, I thought it would be fun to update that post and parse highlights with <em>3.6+</em> and <em>pandas</em>. Janky, but it works:</p>
<div>
<div>
<pre><code><span>import</span> <span>pandas</span> <span>as</span> <span>pd</span>

<span>txt</span> <span>=</span> <span>&quot;&quot;&quot;Sourdough (Robin Sloan)
- Your Highlight on page 187 | Location 2853-2855 | Added on Tuesday, October 2, 2017 8:47:09 PM
The world is going to change, I think—slowly at first, then faster than anyone expects.
==========
Sapiens (Yuval Noah Harari)
- Your Highlight on page 196 | Location 2996-2997 | Added on Tuesday, October 3, 2017 8:51:09 PM
Evolution has made Homo sapiens, like other social mammals, a xenophobic creature.
==========
Life 3.0 (Max Tegmark)
- Your Highlight on page 75 | Location 1136-1137 | Added on Wednesday, October 11, 2017 6:00:15 PM
In short, computation is a pattern in the spacetime arrangement of particles
==========
&quot;&quot;&quot;</span>

<span>with</span> <span>open</span><span>(</span><span>'clippings.txt'</span><span>,</span> <span>'w'</span><span>,</span> <span>encoding</span><span>=</span><span>'utf-8-sig'</span><span>)</span> <span>as</span> <span>f</span><span>:</span>
    <span>f</span><span>.</span><span>write</span><span>(</span><span>txt</span><span>)</span>

<span>with</span> <span>open</span><span>(</span><span>'clippings.txt'</span><span>,</span> <span>'r'</span><span>,</span> <span>encoding</span><span>=</span><span>'utf-8-sig'</span><span>)</span> <span>as</span> <span>f</span><span>:</span>
    <span>contents</span> <span>=</span> <span>f</span><span>.</span><span>read</span><span>()</span><span>.</span><span>replace</span><span>(</span><span>u'</span><span>\ufeff</span><span>'</span><span>,</span> <span>''</span><span>)</span>
    <span>lines</span> <span>=</span> <span>contents</span><span>.</span><span>rsplit</span><span>(</span><span>'=========='</span><span>)</span>
    <span>store</span> <span>=</span> <span>{</span><span>'author'</span><span>:</span> <span>[],</span> <span>'title'</span><span>:</span> <span>[],</span> <span>'quote'</span><span>:</span> <span>[]}</span>
    <span>for</span> <span>line</span> <span>in</span> <span>lines</span><span>:</span>
        <span>try</span><span>:</span>
            <span>meta</span><span>,</span> <span>quote</span> <span>=</span> <span>line</span><span>.</span><span>split</span><span>(</span><span>')</span><span>\n</span><span>- '</span><span>,</span> <span>1</span><span>)</span>
            <span>title</span><span>,</span> <span>author</span> <span>=</span> <span>meta</span><span>.</span><span>split</span><span>(</span><span>' ('</span><span>,</span> <span>1</span><span>)</span>
            <span>_</span><span>,</span> <span>quote</span> <span>=</span> <span>quote</span><span>.</span><span>split</span><span>(</span><span>'</span><span>\n\n</span><span>'</span><span>)</span>
            <span>store</span><span>[</span><span>'author'</span><span>]</span><span>.</span><span>append</span><span>(</span><span>author</span><span>.</span><span>strip</span><span>())</span>
            <span>store</span><span>[</span><span>'title'</span><span>]</span><span>.</span><span>append</span><span>(</span><span>title</span><span>.</span><span>strip</span><span>())</span>
            <span>store</span><span>[</span><span>'quote'</span><span>]</span><span>.</span><span>append</span><span>(</span><span>quote</span><span>.</span><span>strip</span><span>())</span>
        <span>except</span> <span>ValueError</span><span>:</span>
            <span>pass</span>

<span>df</span> <span>=</span> <span>pd</span><span>.</span><span>DataFrame</span><span>(</span><span>store</span><span>)</span>
<span>print</span><span>(</span><span>df</span><span>.</span><span>to_csv</span><span>(</span><span>index</span><span>=</span><span>False</span><span>,</span> <span>encoding</span><span>=</span><span>'utf-8-sig'</span><span>))</span>
<span># author,quote,title</span>
<span># Robin Sloan,&quot;The world is going to change, I think—slowly at first, then faster than anyone expects.&quot;,Sourdough</span>
<span># Yuval Noah Harari,&quot;Evolution has made Homo sapiens, like other social mammals, a xenophobic creature.&quot;,Sapiens</span>
<span># Max Tegmark,&quot;In short, computation is a pattern in the spacetime arrangement of particles&quot;,Life 3.0</span>
</code></pre>
</div>
</div>
<p>Right now I’m 49 books deep. It’s crunch time, but I can see the end! Look for my annual <em>52 Quotes</em> post in a couple of days!</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://maxhumber.com/kindle-clippings-python"> Max Humber</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/hRjs_t3xzj4" height="1"></a></div>