简体   繁体   中英

parsing strings with escape sequences to another software with python

I am using python for another application, I need to parse some text to another file

totalVolume=10
mainBlockID=75
volumeIDSet=[]
volumeIDSet= list(xrange(1,totalVolume-2))

a="geom.cmd('subtract volume " 
a1= "subtract volume "
b= ' '.join(map(str, volumeIDSet))
c= " from volume %d keep')" %(mainBlockID)

d=a+b+c    
print d
d1=a1+b+c

I get printed on screen, which is correct

geom.cmd("subtract volume 1 2 3 4 5 6 7 8 from volume 75 keep') 

But, I get following error

ERROR: Unrecognized symbol: '%'
ERROR: syntax error (<stdin>, line 26384)
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

when I do

geom.cmd(" %s ") %(d1)

The question is why can I not parse d1 as string? regards

geom.cmd(" %s ") returns a NoneType and not a string.

You probably want

geom.cmd(" %s " %(d1) )

Note that this is equivalent to

geom.cmd(d1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM