首页 > 解决方案 > Python 通过 cursor.execute 和 mysql 损坏导入的 csv 文件

问题描述

我确实通过python中的mysql-connection在MySQL数据库中导入了csv文件,但是出了点问题,我完全不知道究竟是什么以及如何修复它。

例如,考虑 last 和 pre-last 列 (1) - ShortTermScore、(2) - LongTermScore 和 (3) RequestedAmount 列。 在此处输入图像描述

在此处输入图像描述

在那个代码之后

import numpy as np
import pandas as pd
import mysql.connector as mysql

db = mysql.connect(
    host = "localhost",
    user = "sgfgdg",
    passwd = "Psswrd"
)

cursor = db.cursor()
cursor = db.cursor(buffered=True)

cursor.execute("CREATE DATABASE IF NOT EXISTS testdb")

cursor.execute("""USE testdb""")
cursor.execute("""CREATE TABLE IF NOT EXISTS test_task (Project INT, OrderDate DATE NOT NULL, orderid INT, ClientID INT,IsRepeat INT,IsBlocked INT,IsManual INT,AutoDecision INT,ManualApprove INT,IsLoan INT,ShortTermAmount INT,ShortTermPeriod INT,LongTermAmount INT,LongTermPeriod INT,RequestedAmount INT,RequestedPeriod INT,LoanSum INT,Period INT,ShortTermScore FLOAT,LongTermScore FLOAT)""")

cursor.execute("LOAD DATA LOCAL INFILE '/home/man/TTF.csv' INTO TABLE test_task FIELDS TERMINATED BY '\t' ")

cursor.execute("""SELECT *  FROM test_task""")
head_rows = cursor.fetchmany(size=6)
print(head_rows)

我懂了

(0, None, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0.0) <- I assume this is header
(1, None, 1794004, 1040307, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 700, 12, 0, 0.0, 0.085)
(1, None, 1794005, 1305335, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 900, 26, 0, 0.0, 0.017)
(1, None, 1794021, 1174614, 1, 0, 0, 1, 0, 1, 0, 0, 40, 0, 0, 0, 500, 20, 0.0, 500.0)
(1, None, 1794032, 1356306, 1, 0, 0, 1, 0, 1, 0, 0, 40, 0, 0, 0, 0, 40, 0.0, 750.0)
(1, None, 1794057, 1120819, 1, 0, 0, 1, 0, 1, 0, 0, 40, 0, 0, 0, 0, 168, 0.0, 0.0)

为什么第一行和第二行分别只有最后一列的 0.085 和 0.017?

第三行和第四行最后一列的 500.0 和 750.0 而不是 0.926 和 0.679 分别是什么?

为什么第一行和第二行分别是 700 和 900 而不是 6700 和 1900?

为什么带有日期(OrderId)的整列完全消失了?))

这太可怕了......我在任何地方都没有看到同样的情况......

https://drive.google.com/file/d/12yPqkyeS8hfD1sbBSwFKLPqeoa4Ycwdt/view?usp=sharing - 初始 excel 文件。

我使用命令将其转换为 csv

soffice --headless --convert-to csv:"Text - txt - csv (StarCalc)":44,34,76 /home/user/Desktop/TTFfw.xlsx

https://drive.google.com/file/d/1Nzas2LJ-Ol6Fr868xe11YrwjKwxUpoSA/view?usp=sharing - csv 文件

请帮我。

标签: pythonmysqlsql

解决方案


推荐阅读