Nov-05-2021, 04:13 PM
Hi Good People,
How many times do I get to say I'm new to Python, man much to learn: I consistently am getting the following error:
How many times do I get to say I'm new to Python, man much to learn: I consistently am getting the following error:
Error:'latin-1' codec can't encode character '\u2019' in position 281: ordinal not in range(256)
I've tried adding the following to no avail. Any help would be appreciated,connection.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8') connection.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8') connection.setencoding(encoding='utf-8')Here's my code:
import pandas as pd import pyodbc import xport.v56 # Trusted Connection to Named Instance try: connection = pyodbc.connect('DRIVER={SQL Server};SERVER=MyServer\MS_SQLEXPRESS;DATABASE=CheckData;Trusted_Connection=yes;') except Exception as err: print('Exception occured while trying to create a connection ', err) else: try: sql_query = pd.read_sql_query('''EXEC sp_Rtest''', connection) # here, the 'connection' is the variable that contains your database connection information from step 2 df = pd.DataFrame(sql_query) ds = xport.Dataset(df, name='DATA', label='Wonderful data') ds = ds.rename(columns={k: k.upper()[:8] for k in ds}) # Other SAS metadata can be set on the columns themselves. for k, v in ds.items(): v.label = k.title() if v.dtype == 'object': v.format = '$CHAR20.' else: v.format = '10.2' # Libraries can have multiple datasets. library = xport.Library({'DATA': ds}) with open('c:/users/cneal/exasssmple.xpt', 'wb') as f: xport.v56.dump(library, f) except Exception as err: print('Exception occured while fethcing records ', err) else: print('') finally: print('') finally: connection.close()