gpt4 book ai didi

python - python中CSV数据的数据类型识别/猜测

转载 作者:IT老高 更新时间:2023-10-28 21:11:25 33 4
gpt4 key购买 nike

我的问题在于处理大型 CSV 文件中的数据。

我正在寻找基于在该列中找到的值来确定(即猜测)该列的数据类型的最有效方法。我可能正在处理非常困惑的数据。因此,该算法应该具有一定的容错性。

这是一个例子:

arr1 = ['0.83', '-0.26', '-', '0.23', '11.23']               # ==> recognize as float
arr2 = ['1', '11', '-1345.67', '0', '22'] # ==> regognize as int
arr3 = ['2/7/1985', 'Jul 03 1985, 00:00:00', '', '4/3/2011'] # ==> recognize as date
arr4 = ['Dog', 'Cat', '0.13', 'Mouse'] # ==> recognize as str

底线:我正在寻找可以检测到任何一种的 python 包或算法

  • CSV 文件的架构,甚至更好
  • 单个列的数据类型作为一个数组

Method for guessing type of data represented currently represented as strings朝着类似的方向发展。不过,我担心性能,因为我可能要处理许多大型电子表格(数据的来源)

最佳答案

您可能对这个 python 库感兴趣,它可以为您对 CSV 和 XLS 文件进行这种类型猜测:

它很高兴地扩展到非常大的文件,从互联网上流式传输数据等。

还有一个更简单的包装库,其中包含一个名为 dataconverters 的命令行工具:http://okfnlabs.org/dataconverters/ (以及在线服务:https://github.com/okfn/dataproxy!)

类型猜测的核心算法在这里:https://github.com/okfn/messytables/blob/7e4f12abef257a4d70a8020e0d024df6fbb02976/messytables/types.py#L164

关于python - python中CSV数据的数据类型识别/猜测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6824862/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com