如何将我的excel表插入数据库

2024-09-27 23:26:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用pandas将excel表插入数据库(microsoftsqlserver)。我使用了第一个ExcelFile,而不是read\u excel和parse,但是parse不能与read\u excel一起工作。 我更喜欢使用read\u excel,因为文件是按需要形成的

这是我的密码:

if __name__ == '__main__':
    cursor = get_sql_conn().cursor()
    local_files = 'C:\\Users\\dersimw\\Source\Repos\\nordpoolAnalyse\\data\\2011-3.xlsx'
    cols = [1,7,13,19,25, 31, 37, 43, 49, 55, 61, 67, 73, 79, 85, 91, 97, 103, 109, 115, 121, 127, 133, 139]
    excelFile = pd.read_excel(local_files, sheet_name= "01", header = None, usecols= cols)
    print(excelFile.head(6))

这是我要插入数据库的文件:

  --------------------------------------------------------------------------
   |         0       1       2       3    ...        20      21      22      23 |
    ---------------------------------------------------------------------------
   | 0       0       1       2       3   ...        20      21      22      23  |
   | 1     112     112     112     112   ...     227.5     227      52      52  |
   | 2  1573.2  1575.2  1596.1  1603.1   ...    1896.8  1833.8  1728.3  1649.3  |
   | 3     NaN     NaN     NaN     NaN   ...       NaN     NaN     NaN     NaN  |
   | 4     NaN     NaN     NaN     NaN   ...       NaN     NaN     NaN     NaN  |
   | 5  2652.3  2505.9  2662.6  2712.7   ...    1613.2    2932    2962    2897  |
   -----------------------------------------------------------------------------

谢谢你的回答


Tags: 文件name数据库pandasreadparselocalfiles

热门问题