This article mainly introduces the Python skills of the export of Excel instance code, small series feel very good, and now share to everyone, but also for everyone to do a reference. Let's take a look at it with a little knitting.
This article describes the Python skills of the export of Excel instance code, just can use, write to share to everyone
As a data analyst, the following requirements are often encountered.
Extract compliant data from a database or existing text file, do a two-time processing, and the finished data is eventually stored in an Excel table for further two analysis by people in other departments.
Here excel as an essential bridge, the right tools and methods to avoid the processing of the data we will spend time a row of replication paste the past.
Python programming is also a necessary skill for a data analyst, and you never know where your data will come from, how complex filtering, filtering, sorting, and combining processing is required, so mastering a programming language and processing commands for commonly used text files under Linux is a must.
Given the simplicity of Python, the ability to get started, and a wide variety of open source libraries, it is the data analyst's first choice.
Here's a look at the Python-based library records how easy it is to export data to excel with just a few lines of code.
I mentioned the records library in the really Useful Python library.
Author Kenneth Reitz is recognized as one of the best two people in Python field code to write, versatile and youthful.
Records is designed for human SQL query library, can be connected to the backend of the various relational database, no need to care about any details, as long as a URL a SQL statement to take care of everything. It also provides the ability to export various query results to various formats (CSV, XLS, JSON, HTML Tables).
Import Recordsrows = [ {"X": 1, "Y": 2}, {"X": 2, "Y": 3}, {"X": 3, "Y": 4}, {"X": 4, "y": 5}]results = Re Cords. Recordcollection (ITER) with open (' demo.xlsx ', ' WB ') as F: F.write (Results.export (' xlsx '))
The table of Excel format that is generated after running directly is as follows:
Of course, you can also generate CSV files, Python's processing library with CSV, which is much simpler to use than the xlsx format, and does not require the introduction of third-party libraries.
One of the most common scenarios for using the records library here is to read the data from the MySQL database and, after some processing, store the data in Excel or JSON-related text files.