Python字符串错误字符

标签 python string io renderman

首先让我说一下,我试图创建一个最小的示例,但无法复制结果。我尽量不让在这里转储我的所有代码成为一种习惯,但我不确定是否有另一种方式来说明这个问题。我正在编写一个类以将数据从我的应用程序输出为特定格式,但我在代码中得到了错误的结果。

我尝试输出的格式应该是这样的:

Nodestyle "nodeType""name"

“属性类型属性名称”[值]

如果属性链接到另一个节点

“引用属性类型属性名称”[“节点名称:输出属性名称”]

例子:

 Bxdf "PxrDisney" "7fcfd465-87c3-4092-98b9-b67d1655bc32" 
 "color baseColor" [0.20.50.8] 
 "color emitColor" [0.00.00.0] 
 "float subsurface" [0] 
 "reference color subsurfaceColor" ["a3bb37f2-dbb8-407b8a1ac93822ebef7c:resultRGB"] 
 "float metallic" [0] 
 "float specular" [.5] 
  "float specularTint" [0] 
  "float roughness" [.25] 
  "float anisotropic" [0] 
  "float sheen" [0] 
  "float sheenTint" [.5] 
  "float clearcoat" [0] 
  "float clearcoatGloss" [1] 
  "float presence" [1] 
  "int inputAOV" [0] 
  Pattern "PxrHSL" "a3bb37f2-dbb8-407b-8a1a-c93822ebef7c" 
  "color inputRGB" [0.00.00.0] 
  "float hue" [0.0] 
  "float saturation" [1.0] 
  "float luminance" [1.0] 

我得到的结果是这样的:

('Bxdf "PxrDisney" "7fcfd465-87c3-4092-98b9-b67d1655bc32" \n "color baseColor" [0.20.50.8] \n "color emitColor" [0.00.00.0] \n "float subsurface" [0] \n "reference color subsurfaceColor"', '["a3bb37f2-dbb8-407b-8a1a-c93822ebef7c:resultRGB"] \n ')"float metallic" [0] 
 "float specular" [.5] 
 "float specularTint" [0] 
 "float roughness" [.25] 
 "float anisotropic" [0] 
 "float sheen" [0] 
 "float sheenTint" [.5] 
 "float clearcoat" [0] 
 "float clearcoatGloss" [1] 
 "float presence" [1] 
 "int inputAOV" [0] 
Pattern "PxrHSL" "a3bb37f2-dbb8-407b-8a1a-c93822ebef7c" 
 "color inputRGB" [0.00.00.0] 
 "float hue" [0.0] 
 "float saturation" [1.0] 
 "float luminance" [1.0] 

如果在字符串中输出特殊字符和额外项,直到有对另一个节点的引用,它看起来像。如果我链接到另一个属性让我们说 basecolor 它只会给出错误的结果直到 basecolor。如果根本没有连接,则没有问题。为什么字符串会产生这种行为?这是有问题的类(class)。该类采用我单步执行的节点对象数组。

#This will handel the export of the RIB contaning all the shader calls
class exporter ():

    nodes = None
    dicNodes = {}
    outStream = []

    #Method Two: Use shader as a starting point
    #Find the shader
    #go through each attribute on the shader

    #if connected check attrs on connected node

    def __init__(self, incomenodes):
        self.nodes = incomenodes
        dicNodes = {}
        outStream = []

        #create a dic using nids as keys
        for node in self.nodes:
            self.dicNodes[str(node.nid)] = node

        #Run Export Command for each shader
        x = 0
        for node in self.nodes:
            print str(node.nodeType.nType)
            if str(node.nodeType.nType) == 'bxdf':
                self.streamMe(node)

        target = open('/home/jspada20/outStream.RIB', 'w')

        print 'JAMES! There are ', len(self.outStream), 'items in the out stream'
        for line in self.outStream:
            target.write(line)
            print line
        target.close()

    def streamMe(self, node):

        #Layout for the header of a node call is....

        #Pattern "PxrThinFilm" "PxrThinFilm1"
        #Bxdf "PxrDisney" "PxrDisney1"
        #{Type} "{Identifyer}" "{Name/UID}"

        moreNodes = []

        #need to fix lower case letters comming in from XML source
        nodefunction = None
        if str(node.nodeType.nType[0]) == 'p':
            nodefunction = 'Pattern'
        elif str(node.nodeType.nType[0]) == 'b':
            nodefunction = "Bxdf"
        else:
            nodefunction = str(node.nodeType.nType)


        self.outStream.append(nodefunction + ' "' + str(node.nodeType.nName) + '" ' + '"' + str(node.nid) + '" \n ')



        for attr in node.inputs:

            #Check to see if it is connected

            #if not connected:
            if attr.currentConnectedNode is None:

                #Check to see what type the value is. This will govern the export scheme
                if str(attr.argType) == "int":
                    #print '"'+ str(attr.argType), attr.name + '" [' + str(attr.value) + ']'
                    self.outStream[len(self.outStream) - 1] = self.outStream[len(self.outStream) - 1] + '"' + str(attr.argType) + " " + attr.name + '" [' + str(attr.value) + '] \n '
                elif str(attr.argType) == "float":
                    if str(attr.value[len(str(attr.value))-1]) == '.':
                        outVal = str(attr.value) + '0'
                    else:
                        outVal = str(attr.value)
                    #print '"'+ str(attr.argType), attr.name + '" [' + outVal + ']'
                    self.outStream[len(self.outStream) - 1] = str(self.outStream[len(self.outStream) - 1]) + '"' + str(attr.argType) + " " + attr.name + '" [' + str(attr.value) + '] \n '

                elif str(attr.argType) == "color":
                    #print '"'+ str(attr.argType), attr.name + '" [' + str(attr.value.r), str(attr.value.g), str(attr.value.b) + ']'
                    self.outStream[len(self.outStream) - 1] = str(self.outStream[len(self.outStream) - 1]) + '"' + str(attr.argType) + " " + attr.name + '" [' + str(attr.value.r) + str(attr.value.g) + str(attr.value.b) + '] \n '

                elif str(attr.argType) == "string":
                    #print '"'+ str(attr.argType), attr.name + '" ["' + attr.value + '"]'
                    self.outStream[len(self.outStream) - 1] = str(self.outStream[len(self.outStream) - 1]) + '"' + str(attr.argType) + " " + attr.name + '" [' + str(attr.value) + '] \n '

            else:
                self.outStream[len(self.outStream) - 1] = str(self.outStream[len(self.outStream) - 1]) + '"reference ' + str(attr.argType) + " " + attr.name + '"', '["' + str(attr.currentConnectedNode.nid) + ':' + attr.currentConnectedOutput.name + '"] \n '
                moreNodes.append(str(attr.currentConnectedNode.nid))

        for ids in moreNodes:
            self.streamMe(self.dicNodes[ids])

最佳答案

当我将此代码从打印语句转换为将信息存储在列表中时,我漏掉了一个逗号。元组可以这样声明:

我的元组 = "1", "2", "3"

通过在我转换字符串的语句中使用逗号。

将此语句中的逗号更改为 + 解决了该问题。来自 C++,我不习惯可变数据类型!

            self.outStream[len(self.outStream) - 1] = str(self.outStream[len(self.outStream) - 1]) + '"reference ' + str(attr.argType) + " " + attr.name + '"' '["' + str(attr.currentConnectedNode.nid) + ':' + attr.currentConnectedOutput.name + '"] \n '

关于Python字符串错误字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31790246/

相关文章:

c++ - 无需检查即可将标准输入快速读入缓冲区

java - 使用两个不同版本的程序读取/写入相同的 csv

python - 无法从同一 docker swarm 上运行的其他服务连接到 postgres 服务?

python - Jupyter 笔记本中的 Tqdm 4.28.1 "IntProgress not found. Please update jupyter and ipywidgets."

c++ - SIGABRT 二进制读/写

Python - 水平打印两个字符串,带有 |

java - 使用多个定界符Java分割字符串

python - 用python替换文件名字符

python - TFLite 的硬刷操作

string - clojure 删除字符串中模式的最后一个入口