【问题标题】:Problem with the date layout when importing a .csv file into a MySQL database将 .csv 文件导入 MySQL 数据库时出现日期布局问题
【发布时间】:2020-04-01 19:58:17
【问题描述】:

由于我在德国生活,我的 .csv 文件中的数百个日期的布局不同于美式日期,因为我的日期是“DD-MM-YYYY”,我需要将它们中的每一个都转换为“YYYY-MM” -DD'。现在回答我的问题。我怎样才能转换它?现在我收到以下错误:

TypeError:字符串格式化期间并非所有参数都转换

我目前使用的代码(仍在处理中):

#!/usr/bin/python

import csv
import pymysql

mydb = pymysql.connections.Connection(host='localhost', user='user', password='password', database='database', unix_socket="/var/run/mysqld/mysqld.sock", port=3306)

cursor = pymysql.cursors.Cursor(mydb)

csv_data = csv.reader(file('wetterdaten_neu.csv'))

for row in csv_data:
    cursor.execute('INSERT INTO sensoren(date,temp,gartemp,tempdallas,pressure,height,pressurenn,tempdallas2,humidity,taup)''VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',row)
    print row

mydb.commit()
cursor.close()
print "CSV has been imported into the database"

希望你能帮助我。

【问题讨论】:

    标签: python mysql csv


    【解决方案1】:

    也许你可以改变那行代码:

    cursor.execute('INSERT INTO sensoren(date,temp,gartemp,tempdallas,pressure,height,pressurenn,tempdallas2,humidity,taup)''VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',row)
    

    到:

    cursor.execute('INSERT INTO sensoren(DATE_FORMAT(STR_TO_DATE(date, "%m-%d-%Y"), "%Y-%m-%d"),temp,gartemp,tempdallas,pressure,height,pressurenn,tempdallas2,humidity,taup)''VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',row)
    

    【讨论】:

    • 我试过了,现在还有一个错误:ValueError: unsupported format character 'm' (0x6d) at index 53
    • 那么呢: cursor.execute('INSERT INTO sensoren(DATE_FORMAT(STR_TO_DATE(date, \'%m-%d-%\'), \'%Y-%m-%d \'),temp,gartemp,tempdallas,压力,高度,压力nn,tempdallas2,湿度,taup)''值(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',row) 这看起来更像是 python 而不是 mysql 的问题。
    • 不幸的是还是同样的错误:ValueError: unsupported format character 'm' (0x6d) at index 53
    【解决方案2】:

    在致电cursor.execute() 之前,您可以使用datetime.strptime() 转换row 中的日期值:

    # 使用 datetime.strptime()

    dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")

    这将设置 dt = datetime.datetime(2006, 11, 21, 16, 30)。然后,您可以输出数据库所需的任何格式。当然,您可以将示例日期字符串替换为您的 row['date'] 值。

    生成格式化字符串的方法如下所示:

    dt.strftime("%Y-%m-%d")
    

    编辑:为澄清起见,您的代码如下所示:

    import datetime
    
    for row in csv_data:
        # First convert date string into a datetime object
        dt = datetime.strptime(row[0], "%d-%m-%Y")
        # Then save the correctly formatted date string back to the row array
        row[0] = dt.strftime("%Y-%m-%d")
        cursor.execute('INSERT INTO sensoren(date,temp,gartemp,tempdallas,pressure,height,pressurenn,tempdallas2,humidity,taup) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',row)
        print row
    

    【讨论】:

    • 我的问题是有一千行数据,每个人都有另一个日期,我想将每个日期转换为想要的格式
    • 你是说传入的 .csv 文件中有多种不同的日期格式?
    • 数以百计的日期,但它们都采用一种格式,我必须为它们中的每一个进行更改
    • @THORPEDO,我添加了一些示例代码以进行澄清。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 2017-11-15
    相关资源
    最近更新 更多