Loading

关于pandas读取excel文件出现精度丢失的问题

读取xlsx文件,其中一个单元格为“440602199305220000”,类型是number,
通过read_excel读取后,变成了“440602199305219968 ”

column_list = []
df_column = pd.read_excel(path, sheet_name='提货委托书').columns
for i in df_column:
    column_list.append(i)
converter = {col: str for col in column_list}
print(converter)
df_actual = pd.read_excel(path, sheet_name='提货委托书',converters=converter)


df = pd.read_excel(path,engine="calamine", dtype='object',sheet_name='提货委托书')

df = pd.read_excel(path, dtype=str,sheet_name='提货委托书',engine='openpyxl')
df

使用了以上三种方法都还是不是,当把sheet转换成csv的时,读取是正常的。

或者excel中把长数字前面添加一个引号

相同问题:
https://blog.csdn.net/weixin_45091039/article/details/125492404

使用其它库进行测试,发现读取出的值是科学计数法

import pyexcel as pe
path = "/Users/txmmy/my-python-projects/table-analysis/tests/Book1.csv"

# 读取 Excel 文件
data = pe.get_sheet(file_name=path)

from openpyxl import load_workbook

# 加载 Excel 文件
path = "提货委托书.xlsx"
wb = load_workbook(path)
sheet = wb.active

# 读取所有单元格的值,并将科学计数法的数值转换为字符串
for row in sheet.iter_rows(values_only=True):
    row_data = []
    for cell in row:
        row_data.append(cell)
    print(row_data)

也就是出现了一个问题: excel中的长数字,pandas读取会变成科学计数法

解决方法: 主要是从excel数据源格式去解决,因为对于身份证号、银行卡号这种,不应该是number类型,image

posted @ 2025-02-17 00:16  踩坑大王  阅读(33)  评论(0)    收藏  举报