2017-03-17 24 views
0

通過@georg參照頂端答案(我已經下面改編)的位置: Split one file into multiple files based on pattern (cut can occur within lines)發生器「產量」在分割結構的輸入文件產生不同步結果

我發現這是一個潛在的有用的圖案分裂根據初始分隔符將文件分爲多個。 但是,正如一位評論者所指出的,它首先創建一個空白文件,其原因尚不清楚。我認爲這與我遇到的問題有關。我試着通過調用output = next(fs)生成器來打開新的輸出文件之前,通過解析分隔符後面的行來設置文件名。在我的(笨拙的,我不是python master!

但是,這個困境當然是直到分隔符後面的行才知道域名。我最終得到的文件名與包含的數據不同步。

輸入文件包含100多個xml「大樹」,其中的每一個與標準

<?xml version='1.0' encoding='UTF-8'?> 

其次是這樣的線,包括域名

<ns2:domain ... name="atypi.org" ..."> 

這裏是我的開始當前腳本:

#!/usr/bin/python2.7 

import re 

def files(): 
    n = 0 
    while n<12 : 
     n += 1 
     print "**DEBUG** in generator nameFile=%s n=%d \r" % (nameFile, n) 
     yield open('/Users/peterf/Google Drive/2015 Projects-Strategy/Domain Admin/RackDomains/%s.part.xml' % nameFile, 'w') 


filename='/Users/peterf/Google Drive/2015 Projects-Strategy/Domain Admin/RackspaceListDomain.output.xml' 
nameFile='' 
pat ='<?xml' 
namePat=re.compile('<ns2:domain.+ name="(.+?)".+>') 
fs = files() 
outfile = next(fs) 

with open(filename) as infile: 
    for line in infile: 
     m=namePat.search(line) 
     if m: 
      nameFile=m.group(1) 
      print "<---\rin 'if m:' nameFile=%s\r" % (nameFile) 
     if pat not in line: 
#   print "\rin 'pat not in line' line=%s\r" % (line)  
      outfile.write(line) 
     else: 
      items = line.split(pat) 
      outfile.write(items[0]) 
      for item in items[1:]: 
       print "in 'for item' pre next(fs) nameFile=%s\r" % (nameFile) 
       outfile = next(fs) 
       print "in 'for item' post next(fs) nameFile=%s --->\r" % (nameFile) 
       outfile.write(pat + item) 

我的調試清單顯示:

**DEBUG** in generator nameFile= n=1 

in 'for item' pre next(fs) nameFile= 

**DEBUG** in generator nameFile= n=2 

in 'for item' post next(fs) nameFile= ---> 

<--- 
in 'if m:' nameFile=addressing.com 

in 'for item' pre next(fs) nameFile=addressing.com 

**DEBUG** in generator nameFile=addressing.com n=3 

in 'for item' post next(fs) nameFile=addressing.com ---> 

<--- 
in 'if m:' nameFile=alicemcmahon.com 

in 'for item' pre next(fs) nameFile=alicemcmahon.com 

**DEBUG** in generator nameFile=alicemcmahon.com n=4 

in 'for item' post next(fs) nameFile=alicemcmahon.com ---> 

<--- 
in 'if m:' nameFile=alphabets.com 

in 'for item' pre next(fs) nameFile=alphabets.com 

**DEBUG** in generator nameFile=alphabets.com n=5 

in 'for item' post next(fs) nameFile=alphabets.com ---> 

輸出目錄包含這些文件名,從第一個「產量」我想截斷名稱開始......

.part.xml (this has data from 'addressing.com') 
addressing.com.part.xml 
alicemcmahon.com.part.xml 
alphabets.com.part.xml 
americanletterpress.com.part.xml 
americanwoodtype.com.part.xml 
amyshoemaker.com.part.xml 
archaicrevivalbooks.com.part.xml 
archaicrevivalfonts.com.part.xml 
archaicrevivalimages.com.part.xml 
astroteddies.com.part.xml 

我無法弄清楚如何處理這個問題,在這裏生成器正在生成輸出文件,然後才能爲文件獲取適當的名稱。

這裏的輸入文件的一些有代表性的部分:

<?xml version='1.0' encoding='utf-8'?> 
<ns2:domain xmlns:ns3="http://www.w3.org/2005/Atom" xmlns:ns2="http://docs.rackspacecloud.com/dns/api/v1.0" xmlns="http://docs.rackspacecloud.com/dns/api/management/v1.0" id="1204245" name="addressing.com" ttl="300" emailAddress="[email protected]" updated="2012-10-10T21:33:36Z" created="2009-07-25T15:05:39Z"> 
    <ns2:nameservers> 
     <ns2:nameserver name="dns1.stabletransit.com" /> 
     <ns2:nameserver name="dns2.stabletransit.com" /> 
    </ns2:nameservers> 
    <ns2:recordsList totalEntries="5"> 
     <ns2:record id="A-2542579" type="A" name="addressing.com" data="198.101.155.141" ttl="300" updated="2012-10-10T21:33:35Z" created="2010-02-17T05:02:16Z" /> 
    </ns2:recordsList> 
</ns2:domain> 
<?xml version='1.0' encoding='UTF-8'?> 
<ns2:domain xmlns:ns3="http://www.w3.org/2005/Atom" xmlns:ns2="http://docs.rackspacecloud.com/dns/api/v1.0" xmlns="http://docs.rackspacecloud.com/dns/api/management/v1.0" id="2776403" name="alicemcmahon.com" ttl="300" emailAddress="[email protected]" updated="2013-10-21T16:43:17Z" created="2011-05-01T03:01:51Z"> 
    <ns2:nameservers> 
     <ns2:nameserver name="dns1.stabletransit.com" /> 
     <ns2:nameserver name="dns2.stabletransit.com" /> 
    </ns2:nameservers> 
    <ns2:recordsList totalEntries="10"> 
     <ns2:record id="A-6895108" type="A" name="alicemcmahon.com" data="216.185.152.144" ttl="300" updated="2013-10-21T16:43:17Z" created="2011-05-01T03:01:51Z" /> 
    </ns2:recordsList> 
</ns2:domain> 
<?xml version='1.0' encoding='UTF-8'?> 
<ns2:domain xmlns:ns3="http://www.w3.org/2005/Atom" xmlns:ns2="http://docs.rackspacecloud.com/dns/api/v1.0" xmlns="http://docs.rackspacecloud.com/dns/api/management/v1.0" id="1204247" name="americanletterpress.com" ttl="300" emailAddress="[email protected]" updated="2012-10-10T21:33:37Z" created="2009-07-25T15:05:41Z"> 
    <ns2:nameservers> 
     <ns2:nameserver name="dns1.stabletransit.com" /> 
     <ns2:nameserver name="dns2.stabletransit.com" /> 
    </ns2:nameservers> 
    <ns2:recordsList totalEntries="5"> 
     <ns2:record id="A-2542581" type="A" name="americanletterpress.com" data="198.101.155.141" ttl="300" updated="2012-10-10T21:33:36Z" created="2010-02-17T05:02:16Z" />   
    </ns2:recordsList> 
</ns2:domain> 
<?xml version='1.0' encoding='UTF-8'?> 
<ns2:domain xmlns:ns3="http://www.w3.org/2005/Atom" xmlns:ns2="http://docs.rackspacecloud.com/dns/api/v1.0" xmlns="http://docs.rackspacecloud.com/dns/api/management/v1.0" id="1204249" name="americanwoodtype.com" ttl="300" emailAddress="[email protected]" updated="2012-10-10T21:33:38Z" created="2009-07-25T15:05:42Z"> 
    <ns2:nameservers> 
     <ns2:nameserver name="dns1.stabletransit.com" /> 
     <ns2:nameserver name="dns2.stabletransit.com" /> 
    </ns2:nameservers> 
    <ns2:recordsList totalEntries="5"> 
     <ns2:record id="A-2542583" type="A" name="americanwoodtype.com" data="198.101.155.141" ttl="300" updated="2012-10-10T21:33:37Z" created="2010-02-17T05:02:16Z" /> 
    </ns2:recordsList> 
</ns2:domain> 

回答

1

你問發電機在最開始產生一個輸出文件:

nameFile='' 
# ... 
outfile = next(fs) 

這就是空白的文件名就在這裏。推遲致電next(fs),直到您的值爲nameFile,而不是之前。

您可以設置outfile = None,而不是和測試None你寫之前:

if pat not in line: 
    if outfile is not None: 
     outfile.write(line) 
else: 
    items = line.split(pat) 
    if outfile is not None: 
     outfile.write(items[0]) 

如果您需要處理線之前,你可以找到你的第一個文件名,保存在緩衝區中的線代替,並清空緩衝區當你第一次創建一個新文件。

不是我認爲你應該使用發電機在所有,你真的使用一個過於複雜的東西。只需在您的循環中直接創建新的文件對象,那就更清晰了。

如果你正在做的是分裂的文件,使用緩衝區,直到你有一個文件名:

buffer = [] 
out_name = '/Users/peterf/Google Drive/2015 Projects-Strategy/Domain Admin/RackDomains/%s.part.xml' 

outfile = None 

with open(filename) as infile: 
    for line in infile: 
     # look for a filename to write to if we don't have one yet 
     if outfile is None: 
      match = namePat.search(line) 
      if match: 
       # New filename, open a file object 
       outfile = open(out_name % match.group(1), 'w') 
       # clear out the buffer, we'll write directly to 
       # the file after this. 
       outfile.writelines(buffer) 
       buffer = [] 

     if '<?xml' in line: 
      # new XML doc, close off the previous one 
      if outfile is not None: 
       outfile.close() 
      outfile = None 

     # line handling 
     if outfile is None: 
      buffer.append(line) 
     else: 
      outfile.write(line) 

if outfile is not None: 
    outfile.close() 
# All lines processed, if there is a buffer left, then we have unhandled lines 
if buffer: 
    print('There were trailing lines without a name') 
    print(*buffer, sep='') 
+0

感謝@martijn!我會修改我的方法。在任何情況下學習發電機都很好;-) – pfraterdeus

+0

確實非常有用!我的問題值得+1嗎? ;-) 正如你所看到的,我幾乎從這裏開始,即使我在1992年建立了我的第一個網站! https://web-beta.archive.org/web/199​​70708013523/http://www.alphabets.com:80/ – pfraterdeus

+0

@pfraterdeus:我相信你打印了我們的名片(我曾經是Jarn的一部分)!只要在那裏堅持下去,從長遠來看,毅力就會得到回報。 –