[Python] 读取csv 文件 设置 dtype 防止数据类型导入错误

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import pandas as pd  

def getFundCodesFromCsv(self):  
    # 从csv文件中获取基金代码清单(可从wind或者其他财经网站导出)
    file_path = os.path.join(os.getcwd(),'fund.csv')  
    fund_code = pd.read_csv(filepath_or_buffer=file_path, encoding='utf-8', dtype='str') 
    # dtype 需要设置,否则基金代码前面是 0 的会以 int 导入,而导致 0 被去掉,代码错误。
    # 比如 001234 --> 1234 
    Code = fund_code["Code"]
    #print ( Code)  
    return Code  
  • 文档备忘
  • [Python] 读取csv 文件 设置 dtype 防止数据类型导入错误已关闭评论
评论关闭

return top