How do I use Python to read and manipulate file suffixes?

Source: Internet
Author: User

Recently in the analysis of a project, saw a suffix ". SQLite" Data file, because the previous did not contact, thinking how to use Python to open and carry out data analysis and processing, so a little research.

SQLite is a very popular relational database, because it is very light, so it is used by a large number of applications.

Like a CSV file, SQLite can store data in a single data file for easy sharing with other people. Many programming languages support the processing of SQLite data, and the Python language is no exception.

Sqlite3 is a standard library of Python that can be used to process SQLite databases.

Creating and manipulating database files with Sqlite3

For database SQL statements, this article will use the most basic SQL statements, should not affect reading. For further information, refer to the following Web site:

Next, let's apply the Salite3 module to create SQLite data files, as well as data read and write operations. The main steps are as follows:

    1. Establish a connection to the database, create a database file (. sqlite file)

    2. Creating Cursors (Cursor)

    3. Create a data table (table)

    4. Inserting data into a data table

    5. Querying data

The demo code is as follows:

Import Sqlite3with sqlite3.connect (' test_database.sqlite ') as con:    c = con.cursor ()    c.execute ("' CREATE TABLE Test_table    (date text, city text, value Real) ") for table in C.execute (" Select name from Sqlite_master WHERE type= ' Ta Ble '):        print ("Table", Table[0])    c.execute ("INSERT into test_table VALUES    (' 2017-6-25 ', ' BJ ', 100) ')    C.execute (' INSERT into test_table VALUES    (' 2017-6-25 ', ' pydataroad ', ') ')    c.execute ("SELECT * FROM Test_table ")    print (C.fetchall ())
Table test_table[(' 2017-6-25 ', ' BJ ', 100.0), (' 2017-6-25 ', ' pydataroad ', 150.0)]

About the data in the SQLite database Visual preview, there are a lot of tools can be implemented, I use the SQLite Studio, is a free use of the tool, do not need to install, download it can be used, interested students can refer to the following links.

Https://sqlitestudio.pl/index.rvt?act=download

The results of the data preview are as follows:

Using pandas to read SQLite data files

From the running results of the above code, it can be seen that the result of the data query is a list composed of tuple. Python's list data can be inconvenient when it comes to further data processing and analysis. Imagine that if there are 1 million rows or more of data in a table in a database, iterating through the list to fetch data is inefficient.

At this point, we can consider using the function provided by pandas to read the relevant data information from the SQLite database file and save it in dataframe for further processing.

Pandas provides two functions that can read information that is suffixed with the ". SQLite" Data file.

    • Read_sql ()

    • Read_sql_query ()

Import pandas as Pdwith sqlite3.connect (' Test_database.sqlite ') as con:# Read_sql_query and Read_ SQL can get data information from the database file via SQL statements df = pd.read_sql_query ("SELECT * from test_table", Con=con) # df = Pd.read_sql ("SELECT * FROM Test _table ", Con=con) print (df.shape) print (    df.dtypes)    print (Df.head ())
 (2, 3) Date objectcity objectvalue float64dtype:object date City Value0 2017-6-25 b J 100.01 2017-6-25 pydataroad 150.0 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.