Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print variable between "
#1
Hi all,

I'm trying to print variable between " (double quotes) .
Code is below:

import cx_Oracle as oracledb
import xml.etree.ElementTree as ET
def output_type_handler(cursor, name, default_type, size, precision, scale):
    if default_type == oracledb.CLOB:
        return cursor.var(oracledb.LONG_STRING, arraysize=cursor.arraysize)
    if default_type == oracledb.BLOB:
        return cursor.var(oracledb.LONG_BINARY, arraysize=cursor.arraysize)

conn = oracledb.connect("/", mode=oracledb.SYSASM)
conn.outputtypehandler = output_type_handler
prev_name = None
cursor = conn.cursor()
cursor.execute("select cellname, confval from v$cell_config where conftype='CELL' order by cellname desc")
print (cursor)
for val in cursor:
    print('"%s"' % val)
    print("\n #########################################") 
I tried {val}, [val], between commas and without but cannot get it. I would need """ (3 double quotes) at the beggining and at the end of variable output.
Reply
#2
You forgot to tell us what the problem is. Do you get an error message? Then show the complete error message. Is the output not as expected? Then show (a sample of) the output and the desired output.
Reply
#3
Error is :

Error:
Traceback (most recent call last): File "<stdin>", line 2, in <module> TypeError: not all arguments converted during string formatting >>>
I could run using :

print ('"""', val)


But is shows all these at the beggining:

Quote:('"""', '<?xml version="1.0"

And I want only something like:
Quote:"""<?xml version="1.0" encoding="utf-8" ?>
<cli-output>
<version>1.0</version>
<timestamp>1646925662329</timestamp>
<context cell="celadm06"/>
<cell> <name>celadm06</name>
<accessLevelPerm>remoteLoginEnabled</accessLevelPerm>
<bbuStatus>normal</bbuStatus>
<cpuCount>96/96</cpuCount>
</cell>
</cli-output>"""
Reply
#4
I found a way that works:

print('"""{var}"""'.format(var=val))
Reply
#5
If you want to parse the xml code contained in val, you don't need the triple quote, nor do you need to print val. The triple quotes are only a syntactic token to help write LITERAL python strings. They are not part of the string itself.
>>> x = """hello world"""
>>> y = 'hello world'
>>> x == y
True
paulo79 likes this post
Reply
#6
If you want to print quotes just escape the quotes to make them a normal character.
print("\"{}\"".format("Hi"))
Output:
"Hi"
paulo79 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print variable without '' and spaces arnonim 1 737 Jan-30-2023, 05:23 PM
Last Post: deanhystad
  Problem with print variable in print.cell (fpdf) muconi 0 670 Dec-25-2022, 02:24 PM
Last Post: muconi
  Remove a space between a string and variable in print sie 5 1,801 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Can you print a string variable to printer hammer 2 1,963 Apr-30-2022, 11:48 PM
Last Post: hammer
  When I print a key from dict it prints it but when I try to assign it to a variable I stefanvelikov 3 2,380 Nov-27-2020, 01:29 PM
Last Post: stefanvelikov
  Print variable values from a list of variables xnightwingx 3 2,652 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Print part of a variable rs74 4 2,606 Jul-27-2020, 04:39 PM
Last Post: rs74
  How to print variable name? mayadob 11 7,709 Oct-21-2019, 01:34 AM
Last Post: ichabod801
  How do I print a returned variable calculated in another function? RedSkeleton007 3 3,546 Jul-10-2018, 12:10 PM
Last Post: buran
  Empty variable when using print before readline fstefanov 3 3,675 Oct-23-2017, 02:22 AM
Last Post: fstefanov

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020