Skip to main content
https://badge.fury.io/py/mwsql.svg https://github.com/mediawiki-utilities/python-mwsql/actions/workflows/test.yml/badge.svg https://readthedocs.org/projects/ansicolortags/badge/?version=latest

Overview

mwsql provides utilities for working with Wikimedia SQL dump files. It supports Python 3.9 and later versions.

mwsql abstracts the messiness of working with SQL dump files. Each Wikimedia SQL dump file contains one database table. The most common use case for mwsql is to convert this table into a more user-friendly Python Dump class instance. This lets you access the table’s metadata (db names, field names, data types, etc.) as attributes, and its content – the table rows – as a generator, which enables processing of larger-than-memory datasets due to the inherent lazy/delayed execution of Python generators.

mwsql also provides a method to convert SQL dump files into CSV. You can find more information on how to use mwsql in the usage examples.

Installation

You can install mwsql with pip:

$ pip install mwsql

Basic Usage

>>> from mwsql import Dump
>>> dump = Dump.from_file('simplewiki-latest-change_tag_def.sql.gz')
>>> dump.head(5)
['ctd_id', 'ctd_name', 'ctd_user_defined', 'ctd_count']
['1', 'mw-replace', '0', '10453']
['2', 'visualeditor', '0', '309141']
['3', 'mw-undo', '0', '59767']
['4', 'mw-rollback', '0', '71585']
['5', 'mobile edit', '0', '234682']
>>> dump.dtypes
{'ctd_id': int, 'ctd_name': str, 'ctd_user_defined': int, 'ctd_count': int}
>>> rows = dump.rows(convert_dtypes=True)
>>> next(rows)
[1, 'mw-replace', 0, 10453]

Known Issues

Encoding errors

Wikimedia SQL dumps use utf-8 encoding. Unfortunately, some fields can contain non-recognized characters, raising an encoding error when attempting to parse the dump file. If this happens while reading in the file, it’s recommended to try again using a different encoding. latin-1 will sometimes solve the problem; if not, you’re encouraged to try with other encodings. If iterating over the rows throws an encoding error, you can try changing the encoding. In this case, you don’t need to recreate the dump – just pass in a new encoding via the dump.encoding attribute.

Parsing errors

Some Wikimedia SQL dumps contain string-type fields that are sometimes not correctly parsed, resulting in fields being split up into several parts. This is more likely to happen when parsing dumps containing file names from Wikimedia Commons or containing external links with many query parameters. If you’re parsing any of the other dumps, you’re unlikely to run into this issue.

In most cases, this issue affects a relatively very small proportion of the total rows parsed. For instance, Wikimedia Commons page dump contains approximately 99 million entries, out of which ~13.000 are incorrectly parsed. Wikimedia Commons page links on the other hand, contains ~760M records, and only 20 are wrongly parsed.

This issue is most commonly caused by the parser mistaking a single quote (or apostrophe, as they’re identical) within a string for the single quote that marks the end of said string. There’s currently no known workaround other than manually removing the rows that contain more fields than expected, or if they are relatively few, manually merging the split fields.

Future versions of mwsql will improve the parser to correctly identify when single quotes should be treated as string delimiters and when they should be escaped. For now, it’s essential to be aware that this problem exists.

Project information

mwsql is released under the GPLv3. You can find the complete documentation at Read the Docs. If you run into bugs, you can file them in our issue tracker. Have ideas on how to make mwsql better? Contributions are most welcome – we have put together a guide on how to get started.

Metadata

Release files for mwsql 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mwsql 1.0.4
File Size Uploaded
mwsql-1.0.4.tar.gz 22.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mwsql 1.0.4
File Interpreter ABI Platform
mwsql-1.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 44.9 kB

Release files / mwsql-1.0.4.tar.gz

Download URL mwsql-1.0.4.tar.gz
Size 22.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c3b24603bda93cdde6c6f3d600805d6a4df4b7bc274aaf24127cf6714e914a0d
BLAKE2b-256 checksum
How to use checksums
2c1555506e21dadf01c2a69639666c795e945c6dfea322d63b30b5a7121f55ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1019-azure

Release files / mwsql-1.0.4-py3-none-any.whl

Download URL mwsql-1.0.4-py3-none-any.whl
Size 22.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ab9ad290be66c13848a0e9d7fc4072160430a9a6ca5fa9c8cbb406e7ebc12452
BLAKE2b-256 checksum
How to use checksums
d6415be5b1bb09f091871971a20b15ac2a055164e85b5958855620802619ff8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1019-azure

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 release files

1.0.3

2 release files

1.0.0

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page