SQL Server Python, ADO GetRows(): When reading from sql numeric values comes with comma as a decimal seperator

tgabmvqs  于 2023-08-02  发布在  Python
关注(0)|答案(1)|浏览(109)

I have written Python code to get data from sql server similar to this example shown here: https://mail.python.org/pipermail/python-list/2002-March/131737.html

However, when I read data into Python for numerical values it uses comma as a decimal seperator, so all my numerical columns turns into string. When I acces data from Microsoft SQL Server Management Studio it shows period mark / dot / '.' as decimal seperator which is the desired way. Also, it shows that data is saved correctly. How do I fix this problem?

My computer language is Swedish maybe that is part of the probem? I have tried to see if I can find the answer here but without luck: IIS 7 - floats returning with commas instead of periods

If you want to see the exact code then it is:

import win32com.client
import pandas as pd
def msql2pandas(query_,datasource_):
    adoConn = win32com.client.Dispatch('ADODB.Connection')
    adoConn.Open("Provider = SQLOLEDB; Data Source = {}; Integrated Security = SSPI".format(datasource_))

    (adoRS, succes) = adoConn.Execute(query_)
    adoRS.MoveFirst()
    data = []
    cols = []
    data_type = []
    n_col = adoRS.Fields.Count
    for i in range(n_col):
        cols.append(adoRS.Fields.Item(i).Name)
        data_type.append(adoRS.Fields.Item(i).Type)

    while not adoRS.EOF:
        temp = [[i[0] for i in adoRS.GetRows(1)]]
        assert len(temp[0]) == n_col
        data.extend(temp)

    return {'data': pd.DataFrame(data = data, columns = cols), 'data_type': data_type}
vlju58qv

vlju58qv1#

Try to change the decimal separator in your locale options (Control Panel | Region | Additional Settings | Decimal Symbol).

相关问题