0

Python で作成した一連のファイル (メタデータ/xml レコードをテキストに変換) からテキスト データを Excel にインポートしています。テキストが単に段落内にあるポイントに新しい行が挿入されることを除いて、ほとんど問題なく動作します。これは、ファイル作成プロセスの問題です。

データを自動的に消去して、エスケープ/新しい文字に遭遇するまでデータを同じ行に維持することは可能ですか?

このサイトでは添付が許可されていないため、ここに例を添付しました。

  1. anz*_log.txt -- 区切り文字として「^」を使用している生のテキスト ファイル。Excelがこれを使用して、これが存在する場合にのみ新しい行を作成できる場合、既知の各行の最後に別の文字を追加するように強制できます。
  2. anz*_xml.xls Excel インポート - ワークシート (*log) 生のインポート データ) を削除し、式を使用して値を適切に取得した場所を消去しました。
  3. rowChar_anz*log.txt - 新しい行であることを示すために、各行の先頭に ':;:' を含む未加工のテキスト ファイル (1 と同じですが、行の区切り文字が追加されています)

これは単なるテスト データセットであり、何千ものファイルでこれを実行する必要があります。行 9、13、54 などの問題を参照してください。

Python (または必要に応じて cygwing/SED) を使用して

  1. 「行頭」文字列「:;:」および「行末」文字列「;:;」を探します。
  2. 両方が単一の行に存在しない場合は、行を前の行に追加します

別の方法として (そして理想的には)、次のコードを使用してファイルを作成している間にこれを行うことはできますか? おそらく re.compile を使用していますか(クエリ CSV のように、元の CSV と結果を単一の CSV Python に書き込みます)?

#-------------------------------------------------------------------------------
# Name:        Convert xml data to csv with anzlic tagged data kept seperate
# Purpose:  Also has an excel template to convert the data into standard columns
#
# Author:      georgec@atgis.com.au
#
# Created:     05/03/2013
# Copyright:   (c) ATGIS. georgec 2013
# Licence:     Creative Commons
#-------------------------------------------------------------------------------

import os, xml, shutil, datetime
from xml.etree import ElementTree as et

SourceDIR=r'L:\Vector_Data'
rootDir=os.getcwd()
log_name='vector'
x=0

def locatexml(SourceDIR,x, rootDir):
    xmllist=[]
    for root, dirs, files in os.walk(SourceDIR, topdown=False):
        for fl in files:
            currentFile=os.path.join(root, fl)
            ext=fl[fl.rfind('.')+1:]
            if ext=='xml':
                xmllist.append(currentFile)
                print currentFile
                x+=1
                try:
                    processxml(currentFile,x, rootDir)
                except:
                    print "Issue with file: "+ currentFile
                    log=open(rootDir+'\\'+log_name+'issue_xml_log.txt','a')
                    log.write(str(x)+'^'+currentFile+'\n')
                    log.close

    print "finished"
    return xmllist, x, currentFile

def processxml(currentFile,x, rootDir):
    from lxml import etree
    seperator='^'
    with open(currentFile) as f:
        tree = etree.parse(f)
    xmltaglist=[]
    for tagn in tree.iter(tag=None):
        #print tagn.tag
        xmltaglist.append(tagn.tag)
    if 'anzmeta' in str(tree.getroot()):
        log=open(rootDir+'\\'+log_name+'anzmeta_xml_log.txt','a')
        log.write(':;:'+seperator+str(x)+seperator+currentFile+seperator)
        for xmltag in xmltaglist:
            for element in tree.iter(xmltag):
                #print element[x]
                for child in element.getchildren():
                    print "{0.tag}: {0.text}".format(child)
                    log.write("{0.tag}".format(child)+"::"+"{0.text}".format(child)+seperator)
        log.write('\n')
        log.close
    else:
        print currentFile+" not an anzlic metadata file...logging seperately"
        log=open(rootDir+'\\'+log_name+'non_anzmeta_xml_log.txt','a')
        log.write(':;:'+seperator+str(x)+seperator+currentFile+seperator)
        for xmltag in xmltaglist:
            for element in tree.iter(xmltag):
                #print element[x]
                for child in element.getchildren():
                    print "{0.tag}: {0.text}".format(child)
                    log.write("{0.tag}".format(child)+"::"+"{0.text}".format(child)+seperator)
        log.write('\n')
        log.close

locatexml(SourceDIR,x, rootDir)
4

1 に答える 1