I have an excel file that I want to push a sheet to database SQL
, But before to push the data I want to replace or remove all Special chacater like brackets
()
, %
and Spaces
..
So for example I have a column name like RRC Setup Success Rate(%)
so I want to make it like this RRCSetupSuccessRate
before to push them to database MSSQL
as Here's my Simple Code looks like:
import pyodbc
import pandas as pd
import os
from sqlalchemy import create_engine
# connect db
engine = create_engine('mssql+pyodbc://xxxxxxxxxxx\xxxxxxxxxx/myDB?driver=SQL+Server+Native+Client+11.0')
cursor = engine.raw_connection().cursor()
mydir = (os.getcwd()).replace('\\', '/') + '/'
lte_details = pd.read_excel(r'' + mydir + 'input.xlsx', sheet_name='LTE Details')
lte_details.columns = lte_details.columns.str.replace(' ', '')
# reading and insert one file at a time
for file in os.listdir('.'):
# only process excels files
file_basename, extension = file.split('.')
if extension == 'xlsx':
lte_details.to_sql(file_basename.lower(), con=engine.connect(), if_exists='replace')
I tried to create some thing like this
lte_details.columns = lte_details.columns.str.replace('\(%\)', '').str.replace(' ','').str.lower()
but I want to add one more thing, that removes all the brackets in the columns name