Maguro 是一个用于 DSV 文件的 Python 包装器。
项目描述
马古罗
Maguro 是一个用于 DSV 文件的 Python 包装器。
其他行为类似于原生 Python 列表,下面的教程仅涵盖 Maguro 特定的附加功能。
正式发布
Maguro 现在可以通过在 Python 就绪环境中运行 pip 命令,通过 PyPi 用于您的 Python 项目。
pip install maguro --upgrade
当前版本是 1.1.0,但更多更新即将推出。这与 Python 3.9 版或最新版本兼容。
包导入
from maguro import Maguro
基本用法
dataset = Maguro("dataset.csv")
自定义编码
dataset = Maguro("dataset.csv", encoding="utf-8")
自定义分隔符
dataset = Maguro("dataset.tsv", delimiter="\t")
清除
dataset.clear()通过 using方法删除列表中的所有项目。
添加项目
用于dataset.append(value)在列表中添加新项目。
仅在唯一时添加
dataset.append(value, unique=True)如果项目尚未在列表中,则用于添加。
排序
用于dataset.sort()按字母顺序对列表进行排序。
撤销
用于dataset.reverse()反转列表。
除去项目
用于dataset.pop(index)删除列表中的第一个出现。
格式化字符串
dataset.pack()通过使用方法返回一个格式化的字符串,由指定的分隔符连接。
原始清单
list通过使用dataset.unpack()方法返回一个原始列表(数据类型)。
循环项目
for item in dataset:
print(item)
除去项目
删除现有(或不存在)的值。用法:dataset.remove(value)
插入项目
在特定索引处插入数据用法:dataset.insert(index, value)
加载列表
将新数据加载到 Maguro 对象中将替换以前的内容。用法:dataset.load(iterable)
扩展列表
扩展原始列表遵循相同的列表语法。用法:dataset.extend(iterable)
删除重复项
Maguro 利用 Pythonlist(set())转换来删除重复项。用法:dataset.distinct()
创建二维数组
test = Maguro("temp/03b-2d.csv", delimiter=",", newline="\n")
test.append(["Juan", 23, "Male", 72, 168, False])
test.append(["Pedro", 22, "Male", 68, 172, True])
test.append(["Maria", 19, "Female", 56, 162, True])
对字符串强制引用
test = Maguro("temp/9-tab-separated-values.tsv", delimiter="\t", newline="\n", quote_strings=True)
test.clear()
test.append(["a", "b", "c", "d", "e"])
test.append(["1", "2", "3", "4", "5"])
test.append([1, 2, 3, 4, 5])
print(test.unpack())
将Yes、y、No和转换n为等效的布尔数据类型(仅限运行时)
test = Maguro("temp/04-booleans.csv", delimiter=",", newline="\n", allow_boolean=True)
标头方法
# Get the header
test = Maguro("temp/12a-header.csv", delimiter=",", newline="\n", quote_strings=True, has_header=True)
print(test.get_header())
# Replace with new header
# equivalent to: test[0] = [*]
test = Maguro("temp/12b-header.csv", delimiter=",", newline="\n", quote_strings=True, has_header=True)
test.set_header(["earthquakeId", "occurred_on", "latitude", "longitude", "depth", "magnitude", "calculation_method", "network_id", "place", "cause"])
print(test[0])
# Add new header
test = Maguro("temp/12c-header.csv", delimiter=",", newline="\n", quote_strings=True)
test.set_header(["earthquake_id", "occurred_on", "latitude", "longitude", "depth", "magnitude", "calculation_method", "network_id", "place", "cause"])
print(test[0])
print(test[1])
# Remove header if set
# exquivalent to: test = test[1:]
test = Maguro("temp/13-header.csv", delimiter=",", newline="\n", quote_strings=True, has_header=True)
test.behead()
print(test[0])
将所有有效子列表扩展到最大长度
test = Maguro("temp/14-expand.csv", delimiter=",", newline="\n")
test.clear()
test.append(["name", "age", "gender", "address"])
test.append(["Juan", "22", "M"])
test.append(["Pedro", "21", "M", "Mars"])
test.append(["Maria", "18", "F", "Earth", "Blue"])
test.append("Soledad")
test.expand()
print(test.pack())
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。