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

Python 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 (CSV) 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.

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 fields. 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.

Here’s a simple example where the fields are Name, Email, and Country. In this example, the CSV data includes a field definition as the first row, although that is not always the case.

Name,Email,Country
John Q. Smith,jqsmith@example.com,USA
Petr Novak,pnovak@example.com,CZ
Bernard Jones,bjones@example.com,UK

Reading CSV from spreadsheets


Python helpfully includes a csv 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).

The Python csv module has a built in reader method called DictReader 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 example.csv in the current directory, this code snippet is one way to get at this data:

f = open('example.csv', 'r')
from csv import DictReader
d = DictReader(f)
data = []
for row in d: data.append(row)

Now the data object in memory is a list of OrderedDict objects :

[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')])]

Referencing each of these objects is easy:

>>> print(data[0]['Country'])
USA
>>> print(data[2]['Email'])
bjones@example.com

By the way, if you have to deal with a CSV file with no header row of field names, the DictReader class lets you define them. In the example above, add the fieldnames argument and pass a sequence of the names:

d = DictReader(f, fieldnames=['Name', 'Email', 'Country'])

A real world example


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.

Fortunately, Python also has a helpful random module good for generating random values. The randrange function in the Random 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.

So this small program worked well:

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'])

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!


Photo by Isaac Smith on Unsplash.



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



Possibly Related Threads…
Thread Author Replies Views Last Post
  Fedora - Incremental backup with Butterfly Backup xSicKxBot 0 2 11 hours ago
Last Post: xSicKxBot
  Fedora - Web of Trust, Part 2: Tutorial xSicKxBot 0 6 10-19-2020, 08:41 PM
Last Post: xSicKxBot
  Fedora - systemd-resolved: introduction to split DNS xSicKxBot 0 11 10-16-2020, 10:28 PM
Last Post: xSicKxBot
  Fedora - Web of Trust, Part 1: Concept xSicKxBot 0 13 10-14-2020, 05:14 PM
Last Post: xSicKxBot
  Fedora - Recover your files from Btrfs snapshots xSicKxBot 0 33 10-05-2020, 07:17 PM
Last Post: xSicKxBot
  Fedora - Use dnsmasq to provide DNS & DHCP services xSicKxBot 0 40 09-30-2020, 08:14 PM
Last Post: xSicKxBot
  Fedora - Announcing the release of Fedora 33 Beta xSicKxBot 0 27 09-29-2020, 09:01 PM
Last Post: xSicKxBot
  Fedora - Now available: Fedora on Lenovo laptops! xSicKxBot 0 26 09-25-2020, 07:51 PM
Last Post: xSicKxBot
  Fedora - Installing and running Vagrant using qemu-kvm xSicKxBot 0 47 09-21-2020, 06:22 PM
Last Post: xSicKxBot
  Fedora - Incremental backups with Btrfs snapshots xSicKxBot 0 46 09-15-2020, 01:20 PM
Last Post: xSicKxBot

Forum Jump:

Become a Patron!
[-]
Upcoming Events

[-]
Latest Threads
News - Borat 2 Review Roundup: Very Nice...
Last Post: xSicKxBot
Today 11:18 AM
» Replies: 0
» Views: 2
Mobile - Win yourself a free copy of sha...
Last Post: xSicKxBot
Today 09:07 AM
» Replies: 0
» Views: 2
[Tut] Top 3 Freelance Developer Contract...
Last Post: xSicKxBot
Today 04:49 AM
» Replies: 0
» Views: 2
(Indie Deal) Alien Cat Bundle, RIDE 4 re...
Last Post: xSicKxBot
Today 04:48 AM
» Replies: 0
» Views: 4
News - Super Smash Bros. Ultimate 9.0.1...
Last Post: xSicKxBot
Today 04:48 AM
» Replies: 0
» Views: 3
Unity Mega Bundle X 10th Anniversary Sal...
Last Post: xSicKxBot
Today 01:02 AM
» Replies: 0
» Views: 3
Microsoft - Fundraising and Engagement f...
Last Post: xSicKxBot
Today 01:02 AM
» Replies: 0
» Views: 2
Fedora - Incremental backup with Butterf...
Last Post: xSicKxBot
Today 01:01 AM
» Replies: 0
» Views: 2
News - Review: Horace – A Heart-Warming ...
Last Post: xSicKxBot
Yesterday 11:38 PM
» Replies: 0
» Views: 2
News - Matthew McConaughey Explains The ...
Last Post: xSicKxBot
Yesterday 10:20 PM
» Replies: 0
» Views: 3

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020