如何使用 Python 将 SQL Server 查询输出写入.txt文件中



我是Python的新手,并尝试连接到SQL Server db并将查询的输出放入平面.txt文件中。

一些代码正在工作,但只写入了近 1000 条记录,然后它停止了。

Python 版本:2.7.13。

下面的代码能够将所有 100 万条记录写入 csv 文件而不是.txt问题是文件。

import sys
print sys.path
import pyodbc
import pandas as pd
connection = pyodbc.connect('DRIVER={SQL Server};SERVER=HCR046TW5SQLHCRMIG50016;DATABASE=ENT;UID=pmatsa1;PWD=password@2015_1711;autocommit=True')
print 'Trying to assign cursor connection'
cursor = connection.cursor()
sql = """SELECT 
LEFT(ltrim(ISNULL(IN_OUT_BUILDING_NUM,' '))+REPLICATE(' ', 10) , 10)+
LEFT( ltrim(ISNULL(IN_OUT_ADR_ORIG_SHORT,' '))+REPLICATE(' ', 50) , 50)+
LEFT(ltrim(ISNULL(IN_OUT_ADR_ORIG_CITY,' '))+REPLICATE(' ', 28) , 28)+
LEFT(ltrim(ISNULL(IN_OUT_ADR_ORIG_STATE,' '))+REPLICATE(' ', 2) , 2)+
LEFT(ltrim(ISNULL(IN_OUT_ADR_ORIG_ZIP,' '))+REPLICATE(' ', 9) , 9)
 FROM ADDR_VAL_STAN_PB;"""
DataOut = open("Address_Validation_Input_File.txt", "a+")
cursor.execute(sql)
# Get data in batches
while True:
    # Read the data
    df = pd.DataFrame(cursor.fetchmany(1000))
    # We are done if there are no data
    if len(df) == 0:
        break
    # Let's write to the file
    else:
        df.to_csv(DataOut, header=False)
# Clean up
DataOut.close()
cursor.close()
connection.close()

将以下代码替换为代码中的以下代码while

df_csv=pd.DataFrame()
while True:
# Read the data
     df = pd.DataFrame(cursor.fetchall())
     # We are done if there are no data
     if len(df) == 0:
            break
     # Let's write to the file
     else:
          df_csv.append(df)
df_csv.to_csv('D:/path/test.txt', header=None, index=None, sep=' ', mode='a')

最新更新