Python is one of the most popular and powerful programming languages. Since it is free and open source, it is available to everyone. Most Fedora systems come with the language already installed.Python can be used for a variety of tasks, including working with comma-separated value (CSV) data.CSV files often start out as tables or spreadsheets. This article describes how to work with CSV data in Python 3.
CSV data is just what the name implies.CSV files place data on rows, with values separated by commas. Each line is defined by the same fields. Short CSV files are usually easy to read and understand. However, longer data files or data files with more fields can be difficult to parse with the naked eye, so computers do a better job in this case.
This is a simple example where the fields areName
、Email
respond in singingCountry
In this example, the CSV data has the field definition as the first line. In this example, the CSV data has the field definition as the first line, although this is not always the case.
Name,Email,Country John Q. Smith,jqsmith@,USA Petr Novak,pnovak@,CZ Bernard Jones,bjones@,UK
Reading CSV from a Spreadsheet
Python includes a csv module that reads and writes CSV data. Most spreadsheet applications, whether native (e.g., Excel or Numbers) or Web-based (e.g., Google Sheet), can export CSV data. In fact, many other services that publish form reports can also export to CSV (e.g., PayPal).
The Python csv module has a built-in reader method called DictReader that treats each row of data as an OrderedDict. It requires a file object to access the CSV data. So, if the file above is in the current directory as , then the following snippet is one way to get this data:
f = open('', 'r') from csv import DictReader d = DictReader(f) data = [] for row in d: (row)
Now, the data object in memory isOrderedDict
A list of objects:
[OrderedDict([('Name', 'John Q. Smith'), ('Email', 'jqsmith@'), ('Country', 'USA')]), OrderedDict([('Name', 'Petr Novak'), ('Email', 'pnovak@'), ('Country', 'CZ')]), OrderedDict([('Name', 'Bernard Jones'), ('Email', 'bjones@'), ('Country', 'UK')])]
Referencing these objects is easy:
>>> print(data[0]['Country']) USA >>> print(data[2]['Email']) bjones@
By the way, if you need to work with CSV files that don't have header lines for fieldnames, then the DictReader class lets you define them. In the example above, add the fieldnames parameter and pass a list of names:
d = DictReader(f, fieldnames=['Name', 'Email', 'Country'])
real-life example
I recently wanted to randomly select a winner from a long list of people. The CSV data I extracted from a spreadsheet was a simple list of names and email addresses.
Fortunately, Python has a useful random module that does a good job of generating random values. The randrange function in the Random class of that module is exactly what I needed. You can give it a regular range of numbers (e.g. integers), and a value for the step between them. The function then generates a random result, which means that I can get a random integer (or rather a row number) within the total number of rows of data.
This little program works well:
from csv import DictReader from random import Random d = DictReader(open('')) data = [] for row in d: (row) r = Random() winner = data[(0, len(data), 1)] print('The winner is:', winner['Name']) print('Email address:', winner['Email'])
Obviously, this example is very simple. The spreadsheet itself contains sophisticated ways to analyze the data. However, if you want to do something outside of a spreadsheet application, Python might be the trick!
summarize
to this article on the use of Python to read the data in the spreadsheet example details of the article is introduced to this, more related python read form data content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!