在pycharm中读取和更新XML,但不在命令行中读取和更新

时间:2017-03-11 23:44:09

标签: python xml encoding pycharm python-3.6

我是python和SO的新手。该脚本打开文件夹中的xml文件。使用os.walk迭代集合并打开文件,然后调用函数迭代xml文件并更新xml文件,使用.writexml重写更新的文件。问题是,当我从命令行运行该程序时,它表示存在错误

Traceback (most recent call last):
  File "./XMLParser.py", line 67, in <module>
    xmldoc = minidom.parse(xml)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/minidom.py", line 1918, in parse
    return expatbuilder.parse(file)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/expatbuilder.py", line 928, in parse
    result = builder.parseFile(file)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/expatbuilder.py", line 207, in parseFile
    parser.Parse(buffer, 0)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 5614: ordinal not in range(128)

CODE:

from xml.dom import minidom

import os

import codecs

'''
Function to iterate over the directory that contains the work items
params:
    CoreID of new author,
    x is the path to the workItem.xml file,
    p is the path to the workItem.xml that will be overwritten with new data
'''
def changeauthor(coreid, x, p):
# Gets the content of the xml based within the work item tag.
testcase = x.getElementsByTagName("work-item")[0]

# All fields are stored as a <field> tag with the id attribute being the
# differentiators between them. Fields is a list of all the field tags in the
# document.
fields = testcase.getElementsByTagName("field")

# Loop iterates over the field tags and looks for the one tag where the id
# attribute has a value of author. when this tag is found the tags value is
# updated to the core id passed to the function.
for field in fields:
    attribute = field.attributes['id'].value
    if attribute == "author":
        # print the current author.
        print("Previous Author: ", field.firstChild.data)
        # Set the author to the core id entered into the script
        field.firstChild.data = coreid
        # Print the updated author field
        print("New Author: ", field.firstChild.data)
        # Create a temp file with the same path as the source
        tmp_config = p
        # Open the new temp file with the write mode set.
        with codecs.open(tmp_config, 'w', "utf-8") as f:
        # f = open(tmp_config, 'w')
        # Write the xml into the file at the same location as the orginal
            x.writexml(f)
        # Close the file
        # f.close()

return


while True:
    core = str(input("Enter Core ID of the new author: "))
    core = core.upper()
    spath = str(input("Please enter the full path to the directory of test cases: "))
    count = 0
    confirm = str(input("Confirm path and core id (Y/N or Exit to leave script): "))
    confirm = confirm.upper()
    if confirm == "Y":
        '''Hard code path here and comment out line above asking for input either will work.'''
        # spath = "/Users/Evan/Desktop/workitems-r583233"

        # Loop iterates over the directory. Whenever a workitem.xml file is found the path is stored and the file is
        # parsed. the core ID entered and the path as well as the parsed xml doc are passed to the change author
        # function.
        for roots, dirs, files in os.walk(spath):
            for file in files:
                title = file.title()
                if title == "Workitem.Xml":
                    path = os.path.join(roots, file)
                    with codecs.open(path, 'r+', "utf-8") as xml:
                        xmldoc = minidom.parse(xml)
                    lst = path.split('/')
                    wi = lst[5]
                    print("Updating: ", wi)
                    changeauthor(core, xmldoc, path)
                    count += 1
                    print(wi, "updated succesfully.")
                    print("-------------------------------")
        if count > 0:
            # Print how many test cases were updated.
            print("All Done", count, "workItems updated!")
        else:
            print("Please double check path and try again no workItems found to update!")
   elif confirm == "N":
       continue
   elif confirm == "EXIT":
       break

0 个答案:

没有答案