sabari dass G
sabari dass G

Reputation: 29

Remove newline from string output

try:
    with cx_oracle.connect(user,password,dsn,encoding ="UTF-8") as connection:                      
        cursor = connection.cursor()                                                                  
        cursor.execute("select * from customer")                                                       
        results =cursor.fetchall()                                                                      
        return json.dumps(results,indent=4,sort_keys = True,default=str)                                                   
except Exception as error:                                                                            
    print ("error occurred",error)  

output

'[\n [\n 1456,\n   "ice kle",\n  "2022:02:08 8:06:01",\n  "2022-02-08 8:07:01",\n "fund"\n]'                                                                                                                                                                                                       

Upvotes: 0

Views: 574

Answers (3)

Tulika
Tulika

Reputation: 138

The indent=4 argument in json.dumps call is responsible for the pretty print(with newlines).
Remove the argument if want all of the result in single line.

@Joran 's answer is correct. I don't have the reputation to add a comment so adding it as answer.

Upvotes: 0

Robert Kalapurackal
Robert Kalapurackal

Reputation: 21

Better replace the '\n' with ''
result = result.replace('\n','')

Upvotes: 0

Joran Beasley
Joran Beasley

Reputation: 113988

just dont tell it to format the json

json.dumps(results) instead of json.dumps(results,indent=4...)

if you really just want to keep that json dumps for some reason and instead remove the newlines ... just do

result = result.replace("\n","")

Upvotes: 1

Related Questions