我有一个名为column_types的字典,其值如下
column_types = {'A': 'pa.int32()',
'B': 'pa.string()'
}
我想将字典传递给pyarrow read csv函数,如下所示
from pyarrow import csv
table = csv.read_csv(file_name,
convert_options=csv.ConvertOptions(column_types=column_types)
)
但它给出了一个错误,因为字典中的值是字符串。 下面的声明将不会有任何问题
from pyarrow import csv
table = csv.read_csv(file_name, convert_options=csv.ConvertOptions(column_types = {
'A':pa.int32(),
'B':pa.string()
}))
如何将字典值更改为可执行语句并将其传递到csv.ConvertOptions
我们为什么不使用这样的东西:
有两种方法对我有效,你可以同时使用它们,但是我建议使用第二种方法,因为第一种方法使用
eval()
,并且在用户输入情况下使用它是有风险的。如果您不使用用户提供的输入字符串,也可以使用方法11)使用
eval()
2)通过创建包含特定字符串的可调用函数名的主词典
dict_dtypes
。并进一步使用dict_dtypes
将字符串映射到其相应的函数相关问题 更多 >
编程相关推荐