当我有一个except参数时,我如何不在Python的循环中声明变量?



我试图遍历xml列表上的所有行并将这些行写入csv我需要每个元素值,如果它存在,要写入,管道分隔到行中,否则显示空值。我能够创建标题行并通过使用变量写入第一行数据,(这显然是不正确的,但我对python非常陌生!)感谢任何帮助!顺便说一下,请随意添加任何我可以更有效或更python化的具体内容。

import xml.etree.ElementTree as ET
import sys
import requests
from requests_ntlm import HttpNtlmAuth
import csv
csv.register_dialect(
    'mydialect',
    delimiter = '|',
    quotechar = '"',
    doublequote = True,
    skipinitialspace = True,
    lineterminator = 'n',
    quoting = csv.QUOTE_MINIMAL)
url="http://sharepoint/projects/urp/_vti_bin/owssvr.dll?Cmd=Display&List={8e2de4cf-79a0-4267-8b84-889a5b890b28}&XMLDATA=TRUE"
#url="http://sharepoint/projects/urp/Lists/HITS%20Estimation%20LOE/AllItems.aspx"
password = "#######"
Username = "YYYY\XXXXX"
server_url="http://sharepoint/"
r=requests.get(url, auth=HttpNtlmAuth(Username,password))
data=r.content
tree = ET.fromstring(data) # load the string into a native XML structure
namespaces = {'s': 'uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882','dt': 'uuid:C2F41010-65B3-11d1-A29F-00AA00C14882', 'rs': 'urn:schemas-microsoft-com:rowset', 'z': '#RowsetSchema'}
header_results = tree.findall('./s:Schema/s:ElementType/s:AttributeType', namespaces)
row_results = tree.findall('./rs:data/z:row', namespaces)
with open('c:output.csv','w') as f:
    writer = csv.writer(f, dialect='mydialect')
#This causes the column name to be pipe delimited across the top row of the csv
    Header_Row=""
    for header in header_results:
        try:
            Header_Row += header.attrib['name']+"|"
        except KeyError:
            Header_Row += "NULL|"
    writer.writerow([Header_Row])
#This part needs help - I need each element value, if it exists, to be written, pipe delimited into the row, or else display a null value
#Currently this only returns one row of data because I am declaring the variable in the loop... how do I accomplish this otherwise?
    for result in row_results:
        try:
            urpid = result.attrib['ows_CnELookup_x003a_URPID']
        except KeyError:
            urpid = "NULL"
        try:
            Attachments = result.attrib['ows_Attachments']
        except KeyError:
            Attachments = "NULL"
        try:
            Title = result.attrib['ows_LinkTitle']
        except KeyError:
            Title = "NULL"
        try:
            Area = result.attrib['ows_Area_x0020_Name']
        except KeyError:
            Area = "NULL"
        try:
            Group = result.attrib['ows_Group']
        except KeyError:
            Group = "NULL"
        try:
            HITS_Hours = result.attrib['ows_HITS_x0020_Hours']
        except KeyError:
            HITS_Hours = "NULL"
        try:
            Consult_Hours = result.attrib['ows_Consultant_x0020_Hours']
        except KeyError:
            Consult_Hours = "NULL"
        try:
            Complete = result.attrib['ows_C_x0026_E_x0020_Completed']
        except KeyError:
            Complete = "NULL"
        try:
            Area_Order = result.attrib['ows_Area_x0020_Order']
        except KeyError:
            Area_Order = "NULL"
    SP_Row = urpid, Attachments, Title, Area, Group, HITS_Hours, Consult_Hours, Complete, Area_Order
    writer.writerow(SP_Row)

实际上,如果你把最后两行缩进一层,我想你会得到你想要的。你在代码中的注释提到了"在循环中声明变量",但是Python变量没有声明——唯一的规则是它们必须在使用之前定义,这就是你正在做的。

作为一种更python化的做事方式,try: ... except KeyError:块并不是真正的做事方式——如果你需要从字典中获得一个存储值或默认值(例如,名为d),使用value = d.get(name, default)代替。

此外,在我看来,你的头将有一个额外的|在结束-我会用这个代替:

    Header_Row = [ header.attrib.get('name', 'NULL') for header in header_results ]
    writer.writerow(Header_Row)

我将使用以下代码代替对结果行的循环:

    for results in row_results:
        SP_ROW = [  result.attrib.get(key, 'NULL')
                    for key in [ 'ows_CnELookup_x003a_URPID', 'ows_Attachments',
                                 'ows_LinkTitle', 'ows_Area_x0020_Name', 'ows_Group',
                                 'ows_HITS_x0020_Hours', 'ows_Consultant_x0020_Hours',
                                 'ows_C_x0026_E_x0020_Completed', 'ows_Area_x0020_Order' ] ]
        writer.writerow(SP_ROW)

您的上下文管理器将确保输出文件已关闭,因此这应该是您所需要的。

最新更新