python pymssql returns list comma separated, how to change the separator to i.e pipe?

Question:

I checked pymssql documents for parameters but I couldn’t find what I was looking for. Basically when I execute the cursor with my SQL query, I always receive the list as comma separated. I’d like to change comma to another separator since some name fields contains comma. Is there a way to do that?

This is what I have:

cnxn = pymssql.connect(
        server=db_server, 
        port=12345, 
        database='DataBase', 
        user=username, 
        password=password)

cursor = cnxn.cursor(as_dict=False)


#sql_statement = "SELECT Name FROM table"
               
cursor.execute(sql_statement)
rows=list(cursor.fetchall())

print(rows)

Output:
[(‘Name,1’, ‘20221110’), (‘Name2’, ‘20221115’)]

What I need:

[(‘Name,1’|’20221110’), (‘Name2’|’20221115’)]

Asked By: Mike Curtis

||

Answers:

I’m making an assumption based on your comment about the need for a non-comma separator to avoid conflicts with the commas in your fields that you would be okay with something like this:

rows = [('Name,1', '20221110'), ('Name2', '20221115')]
for r in rows:
    print("|".join(r))

Output:

Name,1|20221110
Name2|20221115

join creates a single concatenated string from a collection of strings that are separated by the specified string.

Answered By: rhurwitz
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.