I am trying to connect python to mssql database and I want the program to list all databases available in mssql, but the output brings all the databases enclosed in brackets.
db_connection = pyodbc.connect(Driver='{ODBC Driver 17 for SQL Server}', database='',
server='servername', Trusted_Connection='yes')
db_cursor = sce.cursor()
db_cursor.execute('select name from sys.databases')
table_rows = db_cursor.fetchall()
df = pd.DataFrame(table_rows)
print(df)
this is the current output:
0
0 [master]
1 [tempdb]
2 [model]
3 [msdb]
4 [StudiesDB]
5 [Studies]
6 [study]
7 [Test]
Any idea on how I can display the databases without brackets?
CodePudding user response:
We can use str.replace
here:
df[0] = df[0].str.replace(r'^\[|\]$', '')
CodePudding user response:
This will remove the []
df = pd.DataFrame({
0 : ['[Master]', '[MSDB]']
})
df[0] = df[0].apply(lambda x : x.replace('[', '')).apply(lambda x : x.replace(']',''))
df