snipt

Ctrl+h for KB shortcuts

PostgreSQL console (psql)

Dump data from postgreSQL and compress it via gzip

psql -d news -c "\copy (select * from cnyes where newstime between '2015-1-1' and '2016-3-31') to stdout WITH (FORMAT CSV, HEADER TRUE, FORCE_QUOTE *)"  | gzip > /home/aha/cnyes.csv.gz
https://snipt.net/embed/7ecf3e734986d2ffd64027234dc64bac/
https://snipt.net/raw/7ecf3e734986d2ffd64027234dc64bac/
7ecf3e734986d2ffd64027234dc64bac
psql
PostgreSQL console (psql)
1
2016-10-01T16:31:17
True
False
False
Apr 09, 2016 at 08:30 AM
/api/public/snipt/145288/
dump-data-from-postgresql-and-compress-it-via-gzip
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="n">psql</span> <span class="o">-</span><span class="n">d</span> <span class="n">news</span> <span class="o">-</span><span class="n">c</span> <span class="s-Name">&quot;\copy (select * from cnyes where newstime between &#39;2015-1-1&#39; and &#39;2016-3-31&#39;) to stdout WITH (FORMAT CSV, HEADER TRUE, FORCE_QUOTE *)&quot;</span> <span class="o">|</span> <span class="n">gzip</span> <span class="o">&gt;</span> <span class="o">/</span><span class="n">home</span><span class="o">/</span><span class="n">aha</span><span class="o">/</span><span class="n">cnyes</span><span class="mf">.</span><span class="k">csv</span><span class="mf">.</span><span class="n">gz</span> </span></pre></div> </td></tr></table>
csv, gzip, postgresql, sql

Python

Dict Reader example

1
2
3
4
5
import csv

with open('your_csv_file.csv','rb') as source_file:
    for line in csv.DictReader(source_file, delimiter=','):
        print line
https://snipt.net/embed/7e4c276714578fa85a0f483b0da2d9f3/
https://snipt.net/raw/7e4c276714578fa85a0f483b0da2d9f3/
7e4c276714578fa85a0f483b0da2d9f3
python
Python
5
2016-09-27T06:47:46
True
False
False
/api/public/snipt/69002/
dict-reader-example
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="kn">import</span> <span class="nn">csv</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;your_csv_file.csv&#39;</span><span class="p">,</span><span class="s">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">source_file</span><span class="p">:</span> </span><span id="L-4"><a name="L-4"></a> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">csv</span><span class="o">.</span><span class="n">DictReader</span><span class="p">(</span><span class="n">source_file</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s">&#39;,&#39;</span><span class="p">):</span> </span><span id="L-5"><a name="L-5"></a> <span class="k">print</span> <span class="n">line</span> </span></pre></div> </td></tr></table>
csv, dictreader

Shell Session

perl convert dos to unix

perl -pi -e 's/\r\n|\n|\r/\r\n/g' file-to-convert  # Convert to DOS
perl -pi -e 's/\r\n|\n|\r/\n/g'   file-to-convert  # Convert to UNIX
https://snipt.net/embed/549871ea3347ad2dd3783eaf8305470e/
https://snipt.net/raw/549871ea3347ad2dd3783eaf8305470e/
549871ea3347ad2dd3783eaf8305470e
shell-session
Shell Session
3
2016-09-26T18:46:17
True
False
False
May 24, 2013 at 03:31 PM
/api/public/snipt/62165/
perl-convert-dos-to-unix
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="go">perl -pi -e &#39;s/\r\n|\n|\r/\r\n/g&#39; file-to-convert # Convert to DOS</span> </span><span id="L-2"><a name="L-2"></a><span class="go">perl -pi -e &#39;s/\r\n|\n|\r/\n/g&#39; file-to-convert # Convert to UNIX</span> </span></pre></div> </td></tr></table>
convert, csv, dos, perl, unix
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.