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,jqsmith@example.com,USA
Petr Novak,pnovak@example.com,CZ
Bernard Jones,bjones@example.com,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', 'jqsmith@example.com'), ('Country', 'USA')]), OrderedDict([('Name', 'Petr Novak'), ('Email', 'pnovak@example.com'), ('Country', 'CZ')]), OrderedDict([('Name', 'Bernard Jones'), ('Email', 'bjones@example.com'), ('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'])
bjones@example.com</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



Possibly Related Threads…
Thread Author Replies Views Last Post
  Fedora - Contribute to Fedora 39 Upgrade, Virtualization, and Cloud Test Day xSicKxBot 0 2,076 09-30-2023, 03:56 AM
Last Post: xSicKxBot
  Fedora - Share your game achievements with Gamerzilla xSicKxBot 0 1,029 09-27-2023, 09:59 AM
Last Post: xSicKxBot
  Fedora - Using Cockpit to graphically manage systems, without installing Cockpit on xSicKxBot 0 915 09-26-2023, 06:41 AM
Last Post: xSicKxBot
  Fedora - Announcing Fedora Linux 39 Beta xSicKxBot 0 937 09-20-2023, 09:48 AM
Last Post: xSicKxBot
  Fedora - Contribute at Passkey Auth, Fedora CoreOS and IoT Test Week xSicKxBot 0 914 09-19-2023, 12:23 PM
Last Post: xSicKxBot
  Fedora - Quick Fedora shirt update and sale of last stock with the old logo xSicKxBot 0 974 09-16-2023, 12:28 PM
Last Post: xSicKxBot
  Fedora - Contribute at the Fedora Linux Test Week for Kernel 6.5 and Toolbx Test Day xSicKxBot 0 982 09-11-2023, 02:47 PM
Last Post: xSicKxBot
  Fedora - Fedora Linux Flatpak cool apps to try for September xSicKxBot 0 954 09-10-2023, 04:59 PM
Last Post: xSicKxBot
  Fedora - Contribute at the Test Week for the Anaconda WebUI Installer for Fedora Wor xSicKxBot 0 956 09-09-2023, 11:54 PM
Last Post: xSicKxBot
  Fedora - Docs workshop: Virtually writing together xSicKxBot 0 998 09-09-2023, 05:08 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:

Forum software by © MyBB Theme © iAndrew 2016