Fundamentals 14 min read

Python Script for Analyzing Lottery Historical Data and Generating Excel Reports

This article demonstrates how to fetch historical lottery data for two games, apply a custom matching rule to identify purchase opportunities, analyze consecutive win patterns over 100 days, and export the aggregated results to an Excel file using Python's requests, xlwt, and basic data‑processing techniques.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Script for Analyzing Lottery Historical Data and Generating Excel Reports

The requirement is to analyze historical data of two lottery games—"极速飞艇" and "极速赛车"—by checking whether the first two draws contain a pair of equal numbers in a diagonal position and then deciding which numbers to bet on in the third draw based on predefined low (01‑05) and high (06‑10) groups.

Data for each day is retrieved via a REST API (e.g., https://api.api68.com/pks/getPksHistoryList.do?date=2021-1-01&lotCode=10037 ) using requests.get with appropriate headers. The JSON response is decoded with chardet and parsed with json.loads .

After reversing the list of draw results, the script iterates through the 1152 data points, compares adjacent draws, and applies the matching rule. Counters N (total possible bets) and n (successful hits) are updated, while record_number , list_data_number , and dict_time_record track consecutive failures and the timestamps when a hit occurs.

<code><span>def reverse_list(lst):
    """Reverse the order of a list"""
    return [ele for ele in reversed(lst)]
</code>

The core loop (simplified) looks like this:

<code>for k in range(1152):
    if k < 1150:
        new_result1 = reverse_list(new_response["result"]["data"])[k]
        new_result2 = reverse_list(new_response["result"]["data"])[k+1]
        new_result3 = reverse_list(new_response["result"]["data"])[k+2]
        data1 = new_result1['preDrawCode'].split(',')
        data2 = new_result2['preDrawCode'].split(',')
        data3 = new_result3['preDrawCode'].split(',')
        for m in range(10):
            # apply matching logic for positions 0, 9, and others
            ...
</code>

After processing a single day, the script prints a summary line (date, total ladders, hits, misses) and writes the numbers to an Excel sheet using xlwt . Columns include date, total ladder count, hit count, miss count, and the counts/timestamps for sequences of 6, 7, 8, … consecutive misses.

<code>wb = xlwt.Workbook()
sh = wb.add_sheet('彩票分析数据处理')
sh.write(0, 0, "日期")
sh.write(0, 1, "梯子数目")
# ... other header writes ...
sh.write(sheet_seek_position, 1, N)
sh.write(sheet_seek_position, 2, n)
sh.write(sheet_seek_position, 3, N - n)
# write sequence statistics
wb.save('极速飞艇彩票分析结果.xls')
</code>

To handle multiple days, a list of date strings (e.g., data_list = ['1-1', '1-2', ...] ) is generated and the above routine is executed for each entry, accumulating results across the whole period.

The same framework can be adapted for the "极速赛车" game by tweaking the matching condition (using the same low/high groups but different position logic). The article concludes that the approach successfully automates lottery data analysis and produces a comprehensive Excel report.

Pythonautomationdata analysisExcellotteryWeb Scraping
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.