odo在csv和mysql之间转换数据

时间:2016-07-08 20:03:49

标签: windows python-3.x csv pandas odo

使用python .csv's模块将其中一个pd.DataFrame转换为odo时,我会收到TypeError

    TypeError: Cannot cast array from dtype('float64') to dtype('int64') 
               according to the rule 'safe'

适用于其他csv's

的代码
# csv table file name
csvNm = 'table.csv'

# convert mysql table to csv
odo_csv = odo(tstConn.connect_string + '::' + tbl , csvNm)

# convert csv to pandas 
odo_df = odo(odo_csv , pd.DataFrame)

这是我到目前为止所做的尝试无效:

import pandas as pd
from odo import odo, resource, discover, convert

odo_csv=odo(tstConn.connect_string + '::' + tbl , csvNm)
csv=resource(csvNm)
ds=discover(csv)

# Convert csv to pandas
odo_df = odo(odo_csv , pd.DataFrame, dshape=ds) 

和此:

odo_df = odo(odo_csv , pd.DataFrame, casting='unsafe')

更新1 看起来我忽略了这个错误中最明显的暗示

pandas\parser.pyx in pandas.parser.TextReader._convert_tokens (pandas\parser.c:11816)()

导致Windows SO中的编码问题。 但这不是:

odo_df = odo(odo_csv , pd.DataFrame, encoding=odo_csv.encoding)

或这项工作

odo_df = odo(odo_csv , pd.DataFrame, encoding='cp1252') 

这种不合时宜的方式(针对我的用例)来自pandas-reading-csv-files(与上面相同的链接)

# Python3
with open('/tmp/test.csv', 'r', encoding='cp1252') as f:
    df = pd.read_csv(f)
    print(df)

不确定下一步该尝试,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

有效的解决方案是:

import pandas as pd
from odo import odo, resource, discover, convert

# convert mysql to csv
odo_csv=odo(raw_dbConn.connect_string + '::' + tblName , csvNm, header=True)

# Get odo resource aka sqlalchemy.Table instance
resc=resource(raw_dbConn.connect_string + '::' + tblName )

# Discover the resc
ds=discover(resc)

# Convert csv to dataframe    
odo_df = odo(odo_csv , pd.DataFrame, dshape=ds ,encoding=odo_csv.encoding)