read模块
读取文件的函数
该模块提供了读取csv和txt文件的函数,并支持并行处理。
batched
¶
Returns a batch of n items at a time.
如果在python3.12版本中, 可以直接使用: from itertools import batched
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
Iterable
|
An iterable object. |
required |
n
|
int
|
The size of each batch. |
required |
Yields: A batch of n items from the iterable.
Source code in src/cfun/read.py
parallel_handle
¶
parallel_handle(
Iterative: Iterable,
func: Callable,
args: tuple = (),
max_workers: Optional[int] = None,
) -> list
并行处理函数的一个简单的模板
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Iterative
|
Iterable
|
要并行处理的迭代对象。 |
required |
func
|
Callable
|
要执行的函数,签名应为 func(item, *args)。 |
required |
args
|
tuple
|
传递给函数的额外参数。 |
()
|
max_workers
|
int
|
并发进程数,默认由系统决定。 |
None
|
Returns:
| Type | Description |
|---|---|
list
|
处理结果列表 |
Example
Source code in src/cfun/read.py
parallel_load_csv
¶
parallel_load_csv(
files: list[str] | list[Path],
encoding: str = "utf-8",
max_workers: Optional[int] = None,
batch_size: int = 6,
) -> pd.DataFrame
并行处理csv文件
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files
|
list[str] | list[Path]
|
文件路径列表 |
required |
encoding
|
str
|
编码格式 |
'utf-8'
|
max_workers
|
int
|
最大工作线程数 |
None
|
batch_size
|
int
|
每批处理的文件数, 当设置很大的时候,即全部文件一起处理时,(前提每个文件都很小), |
6
|
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame |
DataFrame
|
读取的DataFrame |
Source code in src/cfun/read.py
parallel_load_txt
¶
parallel_load_txt(
files: list[str] | list[Path],
encoding="utf-8",
max_workers: Optional[int] = None,
batch_size: int = 6,
) -> str
并行处理txt文件
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files
|
list[str] | list[Path]
|
文件路径列表 |
required |
encoding
|
str
|
编码格式 |
'utf-8'
|
max_workers
|
int
|
最大工作线程数 |
None
|
batch_size
|
int
|
每批处理的文件数, 当设置很大的时候,即全部文件一起处理时,(前提每个文件都很小), |
6
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
读取的文件内容,字符串 |