“+”是什么意思? Python构建请求Xml

时间:2014-02-28 21:01:15

标签: python xml

在每一行的末尾都有一个" + \":

def buildRequestXml(detailLevel, viewAllNodes):
    requestXml = "<?xml version='1.0' encoding='utf-8'?>"+\
              "<AddItemRequest xmlns=\"urn:ebay:apis:eBLBaseComponents\">"+\
              "<RequesterCredentials><eBayAuthToken>" + userToken + "</eBayAuthToken></RequesterCredentials>"

    if (detailLevel != ""):
        requestXml = requestXml + "<DetailLevel>" + detailLevel + "</DetailLevel>"

    requestXml = requestXml + "<Item>"+\
                    "<BuyItNowPrice>10.0</BuyItNowPrice>"+\
                    "<Country>US</Country>"+\
                    "<Currency>USD</Currency>"+\
                    "<Description>This is a test.</Description>"+\
                    "<ListingDuration>Days_7</ListingDuration>"+\
                    "<Location>San Jose, CA</Location>"+\
                    "<PaymentMethods>PaymentSeeDescription</PaymentMethods>"+\
                    "<PrimaryCategory>"+\
                    "  <CategoryID>357</CategoryID>"+\
                    "</PrimaryCategory>"+\
                    "<Quantity>1</Quantity>"+\
                    "<StartPrice>1.0</StartPrice>"+\
                    "<ShippingTermsInDescription>True</ShippingTermsInDescription>"+\
                    "<Title>Test item title</Title>"+\
                "</Item>"+\
              "</AddItemRequest>"
    return requestXml

3 个答案:

答案 0 :(得分:3)

这是一个线路延续运营商。基本上它正在逃避隐形换行符,迫使Python将所有内容视为单行。

e.g。

somevar = 'foo'+\
    'bar'

相同
somevar = 'foo'+'bar'

答案 1 :(得分:2)

\是一个行继续符,表示下一行是当前行的延续

但是,根据PEP-0008样式指南,最好使用括号隐式继续:

  

包装长行的首选方法是在括号,括号和括号内使用Python隐含的行继续。通过在括号中包装表达式,可以在多行中分割长行。这些应该优先使用反斜杠进行续行。


使用括号代替\

requestXml = (requestXml + "<Item>"+
    "<BuyItNowPrice>10.0</BuyItNowPrice>"+
    "<Country>US</Country>"+
    "<Currency>USD</Currency>"+
    "<Description>This is a test.</Description>"+
    "<ListingDuration>Days_7</ListingDuration>"+
    "<Location>San Jose, CA</Location>"+
    "<PaymentMethods>PaymentSeeDescription</PaymentMethods>"+
    "<PrimaryCategory>"+
    "  <CategoryID>357</CategoryID>"+
    "</PrimaryCategory>"+
    "<Quantity>1</Quantity>"+
    "<StartPrice>1.0</StartPrice>"+
    "<ShippingTermsInDescription>True</ShippingTermsInDescription>"+
    "<Title>Test item title</Title>"+
  "</Item>"+
"</AddItemRequest>")

通过将表达式包装在括号中,Python知道继续到下一行。

答案 2 :(得分:0)

\表示续行。