Aks.Soms
Aks.Soms

Reputation: 137

How to format postgres query output using python

I am new to python. Need help to solve below issue.

Using robot framework with python. Postgres DB is deployed on different machine. And I have accessed it using SSHLibrary.

imei_list = obj.executePostgresQueryToGetResult(db_name='XXXXXX', query=select_query_to_get_result)
print "type: ", type(imei_list)
print "lst: ", "\"" +imei_list+ "\""

When select command is executed, output is returned in String type as below:

type:  <type 'str'>
lst:  "
 359750090003937
 359750090003275
 359750090004513
 359750090003804
 359750090003267
 359750090003226
 359750090001865
"

Need to format output as below is String type:

"359750090003937", "359750090003275", "359750090004513", "359750090003804","359750090003267", "359750090003226", "359750090001865"

NOTE: Numbers returned by query may vary.

Upvotes: 0

Views: 252

Answers (1)

user10343866
user10343866

Reputation:

You can do

imei_list = imei_list.split("\n")

in order to get a list from your string. Afterwards just join the list again

out_string = "\"" + "\", \"".join(imei_list) + "\""

which should give you the desired result.

Upvotes: 2

Related Questions