Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Using data from spreadsheets in Fedora with Python

#1
Using data from spreadsheets in Fedora with Python

<div><p><a href="https://python.org">Python</a> is one of the most popular and powerful programming languages available. Because it’s free and open source, it’s available to everyone — and most Fedora systems come with the language already installed. Python is useful for a wide variety of tasks, but among them is processing comma-separated value (<strong>CSV</strong>) data. CSV files often start off life as tables or spreadsheets. This article shows how to get started working with CSV data in Python 3.</p>
<p> <span id="more-30784"></span> </p>
<p>CSV data is precisely what it sounds like. A CSV file includes one row of data at a time, with data values separated by commas. Each row is defined by the same <em>fields</em>. Short CSV files are often easily read and understood. But longer data files, or those with more fields, may be harder to parse with the naked eye, so computers work better in those cases.</p>
<p>Here’s a simple example where the fields are <em>Name</em>, <em>Email</em>, and <em>Country</em>. In this example, the CSV data includes a field definition as the first row, although that is not always the case.</p>
<pre class="wp-block-preformatted">Name,Email,Country
John Q. Smith,[email protected],USA
Petr Novak,[email protected],CZ
Bernard Jones,[email protected],UK</pre>
<h2>Reading CSV from spreadsheets</h2>
<p>Python helpfully includes a <em>csv</em> module that has functions for reading and writing CSV data. Most spreadsheet applications, both native like Excel or Numbers, and web-based such as Google Sheets, can export CSV data. In fact, many other services that can publish tabular reports will also export as CSV (PayPal for instance).</p>
<p>The Python <em>csv</em> module has a built in reader method called <em>DictReader</em> that can deal with each data row as an ordered dictionary (OrderedDict). It expects a file object to access the CSV data. So if our file above is called <em>example.csv</em> in the current directory, this code snippet is one way to get at this data:</p>
<pre class="wp-block-preformatted">f = open('example.csv', 'r')
from csv import DictReader
d = DictReader(f)
data = []
for row in d: data.append(row)</pre>
<p>Now the <em>data</em> object in memory is a list of OrderedDict objects :</p>
<pre class="wp-block-preformatted">[OrderedDict([('Name', 'John Q. Smith'), ('Email', '[email protected]'), ('Country', 'USA')]), OrderedDict([('Name', 'Petr Novak'), ('Email', '[email protected]'), ('Country', 'CZ')]), OrderedDict([('Name', 'Bernard Jones'), ('Email', '[email protected]'), ('Country', 'UK')])]</pre>
<p>Referencing each of these objects is easy:</p>
<pre class="wp-block-preformatted">&gt;&gt;&gt; print(data[0]['Country'])
USA
&gt;&gt;&gt; print(data[2]['Email'])
[email protected]</pre>
<p>By the way, if you have to deal with a CSV file with no header row of field names, the <em>DictReader</em> class lets you define them. In the example above, add the <em>fieldnames</em> argument and pass a sequence of the names:</p>
<pre class="wp-block-preformatted">d = DictReader(f, fieldnames=['Name', 'Email', 'Country'])</pre>
<h2>A real world example</h2>
<p>I recently wanted to pick a random winner from a long list of individuals. The CSV data I pulled from spreadsheets was a simple list of names and email addresses.</p>
<p>Fortunately, Python also has a helpful <em>random</em> module good for generating random values. The <em>randrange</em> function in the <em>Random</em> class from that module was just what I needed. You can give it a regular range of numbers — like integers — and a step value between them. The function then generates a random result, meaning I could get a random integer (or row number!) back within the total number of rows in my data.</p>
<p>So this small program worked well:</p>
<pre class="wp-block-preformatted">from csv import DictReader
from random import Random d = DictReader(open('mydata.csv'))
data = []
for row in d: data.append(row) r = Random()
winner = data[r.randrange(0, len(data), 1)]
print('The winner is:', winner['Name'])
print('Email address:', winner['Email'])</pre>
<p>Obviously this example is extremely simple. Spreadsheets themselves include sophisticated ways to analyze data. However, if you want to do something outside the realm of your spreadsheet app, Python may be just the trick!</p>
<hr class="wp-block-separator" />
<p><em>Photo by <a href="https://unsplash.com/@isaacmsmith?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Isaac Smith</a> on <a href="https://unsplash.com/s/photos/spreadsheets?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a>.</em></p>
</div>


https://www.sickgaming.net/blog/2020/03/...th-python/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016