simtoolsz 0.2.2__tar.gz → 0.2.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. simtoolsz-0.2.6/PKG-INFO +170 -0
  2. simtoolsz-0.2.6/README.md +146 -0
  3. simtoolsz-0.2.6/README_EN.md +146 -0
  4. simtoolsz-0.2.6/docs/special2db_usage.md +106 -0
  5. simtoolsz-0.2.6/examples/special2db_example.py +144 -0
  6. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/examples/zip2db_example.py +1 -1
  7. simtoolsz-0.2.6/pyproject.toml +45 -0
  8. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/src/simtoolsz/__init__.py +4 -3
  9. simtoolsz-0.2.6/src/simtoolsz/db.py +373 -0
  10. simtoolsz-0.2.6/src/simtoolsz/reader.py +486 -0
  11. simtoolsz-0.2.6/test_optimized_reader.py +110 -0
  12. simtoolsz-0.2.6/tests/test_special2db.py +221 -0
  13. simtoolsz-0.2.6/tests/test_special2db_simple.py +157 -0
  14. simtoolsz-0.2.6/uv.lock +133 -0
  15. simtoolsz-0.2.2/PKG-INFO +0 -30
  16. simtoolsz-0.2.2/README.md +0 -18
  17. simtoolsz-0.2.2/README_EN.md +0 -9
  18. simtoolsz-0.2.2/pyproject.toml +0 -29
  19. simtoolsz-0.2.2/src/simtoolsz/io.py +0 -126
  20. simtoolsz-0.2.2/src/simtoolsz/youtube.py +0 -0
  21. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/.github/workflows/publish.yml +0 -0
  22. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/.gitignore +0 -0
  23. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/.python-version +0 -0
  24. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/LICENSE +0 -0
  25. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/docs/DATETIME_CONVERSION.md +0 -0
  26. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/docs/mail_usage_guide.md +0 -0
  27. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/examples/conversion_examples.py +0 -0
  28. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/examples/mail_examples.py +0 -0
  29. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/examples/today_examples.py +0 -0
  30. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/requirements-dev.lock +0 -0
  31. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/requirements.lock +0 -0
  32. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/src/simtoolsz/datetime.py +0 -0
  33. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/src/simtoolsz/mail.py +0 -0
  34. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/src/simtoolsz/utils.py +0 -0
  35. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/tests/test_conversion.py +0 -0
  36. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/tests/test_iso_comprehensive.py +0 -0
  37. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/tests/test_iso_format.py +0 -0
  38. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/tests/test_simple.py +0 -0
  39. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/tests/test_today_optimized.py +0 -0
  40. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/tests/test_which_format.py +0 -0
  41. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/tests/test_zip2db.py +0 -0
  42. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/tests/test_zip2db_simple.py +0 -0
  43. {simtoolsz-0.2.2 → simtoolsz-0.2.6}/tests/verify_unicode_fix.py +0 -0
@@ -0,0 +1,170 @@
1
+ Metadata-Version: 2.3
2
+ Name: simtoolsz
3
+ Version: 0.2.6
4
+ Summary: A simple and convenient toolkit containing useful functions, classes, and methods.
5
+ Project-URL: Homepage, https://github.com/SidneyLYZhang/simtoolsz
6
+ Project-URL: Repository, https://github.com/SidneyLYZhang/simtoolsz.git
7
+ Project-URL: Issues, https://github.com/SidneyLYZhang/simtoolsz/issues
8
+ Author-email: Sidney Zhang <liangyi@me.com>
9
+ License: MulanPSL-2.0
10
+ Keywords: collection,tool
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: License :: OSI Approved :: Mulan Permissive Software License v2 (MulanPSL-2.0)
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
+ Requires-Python: >=3.11
20
+ Requires-Dist: duckdb>=1.4.0
21
+ Requires-Dist: pendulum>=3.1.0
22
+ Requires-Dist: polars>=1.33.1
23
+ Description-Content-Type: text/markdown
24
+
25
+ # simtoolsz
26
+
27
+ <div>
28
+ <img alt="PyPI - License" src="https://img.shields.io/pypi/l/simtoolsz">
29
+ <img alt="PyPI - Version" src="https://img.shields.io/pypi/v/simtoolsz">
30
+ <img alt="Python - Version" src="https://img.shields.io/pypi/pyversions/simtoolsz">
31
+ </div>
32
+
33
+ [English](README_EN.md) | 中文
34
+
35
+ 一个简单、方便的工具集合,均是个人工作中的常用功能。对之前[pytoolsz](https://github.com/SidneyLYZhang/pytoolsz)工具包的精简重构,保留最实用的功能模块。
36
+
37
+ ## 功能特性
38
+
39
+ ### 🕐 时间处理 (`simtoolsz.datetime`)
40
+ - **时间格式转换**: 支持多种时间格式间的相互转换(中文、英文、ISO8601、秒、分钟、小时等)
41
+ - **智能格式识别**: 自动识别输入的时间格式类型
42
+ - **枚举支持**: 提供 `DurationFormat` 枚举类,标准化时间格式处理
43
+ - **时间计算**: 支持时间跨度的计算和转换
44
+
45
+ ### 📧 邮件处理 (`simtoolsz.mail`)
46
+ - **邮件发送**: 支持HTML/纯文本邮件,附件、抄送、密送
47
+ - **邮件接收**: IMAP协议邮件读取,支持主题搜索
48
+ - **编码支持**: UTF-7编码解码,处理国际化邮件
49
+ - **内嵌图片**: 支持HTML邮件中的内嵌图片
50
+
51
+ ### 💾 数据处理 (`simtoolsz.db`)
52
+ - **压缩包数据读取**: 直接从ZIP压缩包读取CSV、Excel、Parquet、JSON数据到DuckDB
53
+ - **特殊格式支持**: 支持TSV、Avro、Arrow等特殊格式文件的数据库转换
54
+ - **批量处理**: 支持多文件批量导入数据库
55
+ - **灵活配置**: 可自定义表名映射和导入参数
56
+
57
+ ### 📖 数据读取 (`simtoolsz.reader`)
58
+ - **多格式读取**: 统一接口读取CSV、TSV、Excel、Parquet、JSON、IPC、Avro等格式
59
+ - **Polars集成**: 基于Polars的高性能数据读取
60
+ - **智能选择**: 根据文件扩展名自动选择合适的读取器
61
+ - **Lazy加载支持**: 支持大数据集的懒加载模式
62
+
63
+ ### 🛠️ 工具函数 (`simtoolsz.utils`)
64
+ - **日期获取**: `today()` 函数,支持时区、格式化、标准datetime对象返回
65
+ - **列表操作**: `take_from_list()` 智能列表元素查找
66
+ - **文件夹操作**: `checkFolders()` 批量文件夹检查和创建
67
+ - **文件查找**: `lastFile()` 基于时间或大小的文件查找
68
+
69
+ ## 安装
70
+
71
+ ```bash
72
+ pip install simtoolsz
73
+ ```
74
+
75
+ ### 核心依赖
76
+
77
+ - Python >= 3.11
78
+ - pendulum >= 3.1.0
79
+ - duckdb >= 1.4.0
80
+ - polars >= 1.0.0
81
+
82
+ ## 快速开始
83
+
84
+ ### 时间格式转换
85
+ ```python
86
+ from simtoolsz.datetime import TimeConversion
87
+
88
+ # 中文时间到秒
89
+ tc = TimeConversion("1天2小时30分钟45秒", "chinese")
90
+ seconds = tc.convert("seconds")
91
+ print(f"1天2小时30分钟45秒 = {seconds}秒")
92
+
93
+ # 秒到中文时间
94
+ tc = TimeConversion(90061, "seconds")
95
+ chinese = tc.convert("chinese")
96
+ print(f"90061秒 = {chinese}")
97
+ ```
98
+
99
+ ### 发送邮件
100
+ ```python
101
+ from simtoolsz.mail import send_email
102
+
103
+ # 发送纯文本邮件
104
+ result = send_email(
105
+ email_account="your@qq.com",
106
+ password="your_password",
107
+ subject="测试邮件",
108
+ content="这是一封测试邮件",
109
+ recipients="friend@example.com"
110
+ )
111
+
112
+ # 发送HTML邮件带附件
113
+ result = send_email(
114
+ email_account="your@gmail.com",
115
+ password="app_password",
116
+ subject="项目报告",
117
+ content="<h1>本月工作报告</h1><p>详见附件</p>",
118
+ recipients=["boss@company.com", "同事<colleague@company.com>"],
119
+ attachments=["report.pdf", "data.xlsx"],
120
+ html_mode=True
121
+ )
122
+ ```
123
+
124
+ ### 数据读取
125
+ ```python
126
+ from simtoolsz.reader import getreader
127
+ import polars as pl
128
+
129
+ # 使用getreader读取CSV文件
130
+ reader = getreader("data.csv")
131
+ df = reader("data.csv")
132
+
133
+ # 读取TSV文件
134
+ df = load_tsv("data.tsv")
135
+
136
+ # Lazy加载大数据集
137
+ lazy_df = load_data("large_data.csv", lazy=True)
138
+
139
+ # 加载压缩数据集
140
+ df = load_data("large_data_archive.tar.gz/data.csv")
141
+ ```
142
+
143
+ ### 压缩包数据导入数据库
144
+ ```python
145
+ from simtoolsz.db import zip2db
146
+
147
+ # 从ZIP文件读取数据到DuckDB
148
+ con = zip2db(
149
+ zip_file="data.zip",
150
+ db_file="output.db",
151
+ table={"users.csv": "用户表", "orders.xlsx": "订单表"}
152
+ )
153
+
154
+ # 查询数据
155
+ tables = con.execute("SHOW TABLES").fetchall()
156
+ print(f"导入的表: {tables}")
157
+ ```
158
+
159
+ ### 工具函数
160
+ ```python
161
+ from simtoolsz.utils import today, take_from_list
162
+
163
+ # 获取当前日期时间
164
+ current = today(addtime=True)
165
+ formatted = today(fmt="YYYY年MM月DD日 HH:mm:ss")
166
+
167
+ # 列表查找
168
+ result = take_from_list("hello", ["he", "world"]) # 返回 "he"
169
+ result = take_from_list([2, 3], [1, 2, 3, 4]) # 返回 2
170
+ ```
@@ -0,0 +1,146 @@
1
+ # simtoolsz
2
+
3
+ <div>
4
+ <img alt="PyPI - License" src="https://img.shields.io/pypi/l/simtoolsz">
5
+ <img alt="PyPI - Version" src="https://img.shields.io/pypi/v/simtoolsz">
6
+ <img alt="Python - Version" src="https://img.shields.io/pypi/pyversions/simtoolsz">
7
+ </div>
8
+
9
+ [English](README_EN.md) | 中文
10
+
11
+ 一个简单、方便的工具集合,均是个人工作中的常用功能。对之前[pytoolsz](https://github.com/SidneyLYZhang/pytoolsz)工具包的精简重构,保留最实用的功能模块。
12
+
13
+ ## 功能特性
14
+
15
+ ### 🕐 时间处理 (`simtoolsz.datetime`)
16
+ - **时间格式转换**: 支持多种时间格式间的相互转换(中文、英文、ISO8601、秒、分钟、小时等)
17
+ - **智能格式识别**: 自动识别输入的时间格式类型
18
+ - **枚举支持**: 提供 `DurationFormat` 枚举类,标准化时间格式处理
19
+ - **时间计算**: 支持时间跨度的计算和转换
20
+
21
+ ### 📧 邮件处理 (`simtoolsz.mail`)
22
+ - **邮件发送**: 支持HTML/纯文本邮件,附件、抄送、密送
23
+ - **邮件接收**: IMAP协议邮件读取,支持主题搜索
24
+ - **编码支持**: UTF-7编码解码,处理国际化邮件
25
+ - **内嵌图片**: 支持HTML邮件中的内嵌图片
26
+
27
+ ### 💾 数据处理 (`simtoolsz.db`)
28
+ - **压缩包数据读取**: 直接从ZIP压缩包读取CSV、Excel、Parquet、JSON数据到DuckDB
29
+ - **特殊格式支持**: 支持TSV、Avro、Arrow等特殊格式文件的数据库转换
30
+ - **批量处理**: 支持多文件批量导入数据库
31
+ - **灵活配置**: 可自定义表名映射和导入参数
32
+
33
+ ### 📖 数据读取 (`simtoolsz.reader`)
34
+ - **多格式读取**: 统一接口读取CSV、TSV、Excel、Parquet、JSON、IPC、Avro等格式
35
+ - **Polars集成**: 基于Polars的高性能数据读取
36
+ - **智能选择**: 根据文件扩展名自动选择合适的读取器
37
+ - **Lazy加载支持**: 支持大数据集的懒加载模式
38
+
39
+ ### 🛠️ 工具函数 (`simtoolsz.utils`)
40
+ - **日期获取**: `today()` 函数,支持时区、格式化、标准datetime对象返回
41
+ - **列表操作**: `take_from_list()` 智能列表元素查找
42
+ - **文件夹操作**: `checkFolders()` 批量文件夹检查和创建
43
+ - **文件查找**: `lastFile()` 基于时间或大小的文件查找
44
+
45
+ ## 安装
46
+
47
+ ```bash
48
+ pip install simtoolsz
49
+ ```
50
+
51
+ ### 核心依赖
52
+
53
+ - Python >= 3.11
54
+ - pendulum >= 3.1.0
55
+ - duckdb >= 1.4.0
56
+ - polars >= 1.0.0
57
+
58
+ ## 快速开始
59
+
60
+ ### 时间格式转换
61
+ ```python
62
+ from simtoolsz.datetime import TimeConversion
63
+
64
+ # 中文时间到秒
65
+ tc = TimeConversion("1天2小时30分钟45秒", "chinese")
66
+ seconds = tc.convert("seconds")
67
+ print(f"1天2小时30分钟45秒 = {seconds}秒")
68
+
69
+ # 秒到中文时间
70
+ tc = TimeConversion(90061, "seconds")
71
+ chinese = tc.convert("chinese")
72
+ print(f"90061秒 = {chinese}")
73
+ ```
74
+
75
+ ### 发送邮件
76
+ ```python
77
+ from simtoolsz.mail import send_email
78
+
79
+ # 发送纯文本邮件
80
+ result = send_email(
81
+ email_account="your@qq.com",
82
+ password="your_password",
83
+ subject="测试邮件",
84
+ content="这是一封测试邮件",
85
+ recipients="friend@example.com"
86
+ )
87
+
88
+ # 发送HTML邮件带附件
89
+ result = send_email(
90
+ email_account="your@gmail.com",
91
+ password="app_password",
92
+ subject="项目报告",
93
+ content="<h1>本月工作报告</h1><p>详见附件</p>",
94
+ recipients=["boss@company.com", "同事<colleague@company.com>"],
95
+ attachments=["report.pdf", "data.xlsx"],
96
+ html_mode=True
97
+ )
98
+ ```
99
+
100
+ ### 数据读取
101
+ ```python
102
+ from simtoolsz.reader import getreader
103
+ import polars as pl
104
+
105
+ # 使用getreader读取CSV文件
106
+ reader = getreader("data.csv")
107
+ df = reader("data.csv")
108
+
109
+ # 读取TSV文件
110
+ df = load_tsv("data.tsv")
111
+
112
+ # Lazy加载大数据集
113
+ lazy_df = load_data("large_data.csv", lazy=True)
114
+
115
+ # 加载压缩数据集
116
+ df = load_data("large_data_archive.tar.gz/data.csv")
117
+ ```
118
+
119
+ ### 压缩包数据导入数据库
120
+ ```python
121
+ from simtoolsz.db import zip2db
122
+
123
+ # 从ZIP文件读取数据到DuckDB
124
+ con = zip2db(
125
+ zip_file="data.zip",
126
+ db_file="output.db",
127
+ table={"users.csv": "用户表", "orders.xlsx": "订单表"}
128
+ )
129
+
130
+ # 查询数据
131
+ tables = con.execute("SHOW TABLES").fetchall()
132
+ print(f"导入的表: {tables}")
133
+ ```
134
+
135
+ ### 工具函数
136
+ ```python
137
+ from simtoolsz.utils import today, take_from_list
138
+
139
+ # 获取当前日期时间
140
+ current = today(addtime=True)
141
+ formatted = today(fmt="YYYY年MM月DD日 HH:mm:ss")
142
+
143
+ # 列表查找
144
+ result = take_from_list("hello", ["he", "world"]) # 返回 "he"
145
+ result = take_from_list([2, 3], [1, 2, 3, 4]) # 返回 2
146
+ ```
@@ -0,0 +1,146 @@
1
+ # simtoolsz
2
+
3
+ <div>
4
+ <img alt="PyPI - License" src="https://img.shields.io/pypi/l/simtoolsz">
5
+ <img alt="PyPI - Version" src="https://img.shields.io/pypi/v/simtoolsz">
6
+ <img alt="Python - Version" src="https://img.shields.io/pypi/pyversions/simtoolsz">
7
+ </div>
8
+
9
+ English | [中文](README.md)
10
+
11
+ A simple and convenient toolkit containing useful functions, classes, and methods. A streamlined refactoring of the previous [pytoolsz](https://github.com/SidneyLYZhang/pytoolsz) toolkit, keeping only the most practical functional modules.
12
+
13
+ ## Features
14
+
15
+ ### 🕐 Time Processing (`simtoolsz.datetime`)
16
+ - **Time Format Conversion**: Supports mutual conversion between multiple time formats (Chinese, English, ISO8601, seconds, minutes, hours, etc.)
17
+ - **Smart Format Recognition**: Automatically identifies the type of input time format
18
+ - **Enum Support**: Provides `DurationFormat` enum class for standardized time format processing
19
+ - **Time Calculation**: Supports calculation and conversion of time spans
20
+
21
+ ### 📧 Email Processing (`simtoolsz.mail`)
22
+ - **Email Sending**: Supports HTML/plain text emails, attachments, CC, BCC
23
+ - **Email Receiving**: IMAP protocol email reading, supports subject search
24
+ - **Encoding Support**: UTF-7 encoding and decoding for handling internationalized emails
25
+ - **Embedded Images**: Supports embedded images in HTML emails
26
+
27
+ ### 💾 Data Processing (`simtoolsz.db`)
28
+ - **Compressed Data Reading**: Directly reads CSV, Excel, Parquet, JSON data from ZIP archives to DuckDB
29
+ - **Special Format Support**: Supports database conversion of special format files like TSV, Avro, Arrow
30
+ - **Batch Processing**: Supports batch import of multiple files to database
31
+ - **Flexible Configuration**: Customizable table name mapping and import parameters
32
+
33
+ ### 📖 Data Reading (`simtoolsz.reader`)
34
+ - **Multi-format Reading**: Unified interface for reading CSV, TSV, Excel, Parquet, JSON, IPC, Avro and other formats
35
+ - **Polars Integration**: High-performance data reading based on Polars
36
+ - **Smart Selection**: Automatically selects appropriate reader based on file extension
37
+ - **Lazy Loading Support**: Supports lazy loading mode for large datasets
38
+
39
+ ### 🛠️ Utility Functions (`simtoolsz.utils`)
40
+ - **Date Acquisition**: `today()` function, supports timezone, formatting, standard datetime object return
41
+ - **List Operations**: `take_from_list()` intelligent list element lookup
42
+ - **Folder Operations**: `checkFolders()` batch folder checking and creation
43
+ - **File Lookup**: `lastFile()` file lookup based on time or size
44
+
45
+ ## Installation
46
+
47
+ ```bash
48
+ pip install simtoolsz
49
+ ```
50
+
51
+ ### Core Dependencies
52
+
53
+ - Python >= 3.11
54
+ - pendulum >= 3.1.0
55
+ - duckdb >= 1.4.0
56
+ - polars >= 1.0.0
57
+
58
+ ## Quick Start
59
+
60
+ ### Time Format Conversion
61
+ ```python
62
+ from simtoolsz.datetime import TimeConversion
63
+
64
+ # Chinese time to seconds
65
+ tc = TimeConversion("1天2小时30分钟45秒", "chinese")
66
+ seconds = tc.convert("seconds")
67
+ print(f"1天2小时30分钟45秒 = {seconds}秒")
68
+
69
+ # Seconds to Chinese time
70
+ tc = TimeConversion(90061, "seconds")
71
+ chinese = tc.convert("chinese")
72
+ print(f"90061秒 = {chinese}")
73
+ ```
74
+
75
+ ### Send Email
76
+ ```python
77
+ from simtoolsz.mail import send_email
78
+
79
+ # Send plain text email
80
+ result = send_email(
81
+ email_account="your@qq.com",
82
+ password="your_password",
83
+ subject="Test Email",
84
+ content="This is a test email",
85
+ recipients="friend@example.com"
86
+ )
87
+
88
+ # Send HTML email with attachments
89
+ result = send_email(
90
+ email_account="your@gmail.com",
91
+ password="app_password",
92
+ subject="Project Report",
93
+ content="<h1>This Month's Work Report</h1><p>See attachment for details</p>",
94
+ recipients=["boss@company.com", "colleague<colleague@company.com>"],
95
+ attachments=["report.pdf", "data.xlsx"],
96
+ html_mode=True
97
+ )
98
+ ```
99
+
100
+ ### Data Reading
101
+ ```python
102
+ from simtoolsz.reader import getreader
103
+ import polars as pl
104
+
105
+ # Read CSV file using getreader
106
+ reader = getreader("data.csv")
107
+ df = reader("data.csv")
108
+
109
+ # Read TSV file
110
+ df = load_tsv("data.tsv")
111
+
112
+ # Lazy loading for large datasets
113
+ lazy_df = load_data("large_data.csv", lazy=True)
114
+
115
+ # Load compressed dataset
116
+ df = load_data("large_data_archive.tar.gz/data.csv")
117
+ ```
118
+
119
+ ### Compressed Data Import to Database
120
+ ```python
121
+ from simtoolsz.db import zip2db
122
+
123
+ # Read data from ZIP file to DuckDB
124
+ con = zip2db(
125
+ zip_file="data.zip",
126
+ db_file="output.db",
127
+ table={"users.csv": "users_table", "orders.xlsx": "orders_table"}
128
+ )
129
+
130
+ # Query data
131
+ tables = con.execute("SHOW TABLES").fetchall()
132
+ print(f"Imported tables: {tables}")
133
+ ```
134
+
135
+ ### Utility Functions
136
+ ```python
137
+ from simtoolsz.utils import today, take_from_list
138
+
139
+ # Get current date and time
140
+ current = today(addtime=True)
141
+ formatted = today(fmt="YYYY-MM-DD HH:mm:ss")
142
+
143
+ # List lookup
144
+ result = take_from_list("hello", ["he", "world"]) # Returns "he"
145
+ result = take_from_list([2, 3], [1, 2, 3, 4]) # Returns 2
146
+ ```
@@ -0,0 +1,106 @@
1
+ # special2db 函数使用指南
2
+
3
+ `special2db` 函数用于将特殊格式的数据文件(TSV、Avro、Arrow)转换为DuckDB数据库。
4
+
5
+ ## 支持的文件格式
6
+
7
+ - **TSV**: 制表符分隔的文本文件
8
+ - **Avro**: Apache Avro格式文件
9
+ - **Arrow**: Apache Arrow格式文件
10
+
11
+ ## 基本用法
12
+
13
+ ### 读取单个TSV文件
14
+
15
+ ```python
16
+ from simtoolsz.db import special2db
17
+
18
+ # 读取单个TSV文件到DuckDB
19
+ con = special2db('data/users.tsv', 'users.db')
20
+
21
+ # 查询数据
22
+ tables = con.execute("SHOW TABLES").fetchall()
23
+ count = con.execute("SELECT COUNT(*) FROM users").fetchone()[0]
24
+ print(f"记录数: {count}")
25
+
26
+ # 关闭连接
27
+ con.close()
28
+ ```
29
+
30
+ ### 使用自定义表名
31
+
32
+ ```python
33
+ # 使用自定义表名
34
+ con = special2db('data/customers.tsv', 'customers.db', table='客户表')
35
+
36
+ # 查询自定义表名的数据
37
+ count = con.execute("SELECT COUNT(*) FROM 客户表").fetchone()[0]
38
+ print(f"客户表记录数: {count}")
39
+ con.close()
40
+ ```
41
+
42
+ ### 处理目录中的多个文件
43
+
44
+ ```python
45
+ # 处理目录中的所有TSV文件
46
+ con = special2db('data_directory', 'all_data.db')
47
+
48
+ # 查看所有创建的表
49
+ tables = con.execute("SHOW TABLES").fetchall()
50
+ print(f"创建的表: {[table[0] for table in tables]}")
51
+
52
+ # 查询特定表
53
+ count = con.execute("SELECT COUNT(*) FROM users").fetchone()[0]
54
+ print(f"users表记录数: {count}")
55
+ con.close()
56
+ ```
57
+
58
+ ### 指定读取参数
59
+
60
+ ```python
61
+ # 指定编码和其他参数
62
+ con = special2db('data/data.tsv', 'output.db',
63
+ encoding='utf-8',
64
+ header=True)
65
+
66
+ # 查询数据
67
+ tables = con.execute("SHOW TABLES").fetchall()
68
+ print(f"创建的表: {[table[0] for table in tables]}")
69
+ con.close()
70
+ ```
71
+
72
+ ## 错误处理
73
+
74
+ 函数会在以下情况下抛出 `ValueError`:
75
+
76
+ 1. **找不到支持的数据文件**
77
+ ```python
78
+ # 目录中没有TSV、Avro或Arrow文件
79
+ con = special2db('empty_directory', 'output.db')
80
+ # ValueError: 未找到支持的数据文件(tsv、avro、arrow)
81
+ ```
82
+
83
+ 2. **不支持的文件格式**
84
+ ```python
85
+ # 尝试读取不支持的文件格式
86
+ con = special2db('data/file.txt', 'output.db')
87
+ # ValueError: 不支持的文件格式: .txt。支持的格式: tsv, avro, arrow
88
+ ```
89
+
90
+ ## 表名处理规则
91
+
92
+ 1. **默认表名**: 使用文件名(不含扩展名)
93
+ 2. **自定义表名**: 通过 `table` 参数指定
94
+ 3. **表名清理**: 自动移除特殊字符,只保留字母、数字和下划线
95
+ 4. **多文件处理**: 每个文件创建一个表,表名基于文件名
96
+
97
+ ## 注意事项
98
+
99
+ 1. **数据库连接**: 函数返回DuckDB连接对象,使用完毕后需要手动关闭
100
+ 2. **表覆盖**: 如果表已存在,会先删除再重新创建
101
+ 3. **文件锁定**: 确保在使用完连接后关闭,避免文件锁定问题
102
+ 4. **临时文件**: 示例中使用临时文件时,注意文件生命周期管理
103
+
104
+ ## 完整示例
105
+
106
+ 参考 `examples/special2db_example.py` 文件获取完整的使用示例。