【问题标题】:Editing timeslot for SQL in Python在 Python 中编辑 SQL 的时隙
【发布时间】:2020-06-01 15:53:21
【问题描述】:

我有一个 excel 文件,其中包含每小时基础值、日期和时间作为时间段。

我想将此 excel 导入 PostgreSQL 数据库,但我需要编辑之前的日期和时间。否则给出错误。

在 excel 中编辑该日期和时间也可以,但我认为在 python 中这样做会容易得多。

我想把那个日期和时间转换成这个时间段:

2018-05-27 00:00:00 

【问题讨论】:

    标签: python sql excel timeslots


    【解决方案1】:

    我像这样解决了它,它奏效了。有人可能想知道解决方案。

    import pandas as pd
    from xlwt import Workbook
    from datetime import datetime
    
    df = pd.read_excel('yourexcell file.xlsx')
    date=[]
    
    for i in df.index:
        start_date = df['date'][i] #date column name in excel file 
        start_time=df['time'][i] #time column name in excel file
    
        date_time = datetime.strptime(start_date, "%d.%m.%Y")
    
        comb=datetime.combine(date_time,start_time)
    
        date.append(comb)
    #dateandtime combined and ready.
    
    
    wb = Workbook()
    sheet = wb.add_sheet('Sheet 1')
    
    for k in range(len(date)):
        sheet.write(k,0,date[k])
    wb.save('datetime_generated'+".CSV")
    print("csv file saved.")
    

    这里是: 输出 2018-05-27 00:00:00

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-11
      • 2018-02-23
      • 2015-06-23
      • 1970-01-01
      • 2016-02-14
      • 2021-04-26
      • 1970-01-01
      相关资源
      最近更新 更多