簡體   English   中英

python中的filename參數的execfile問題

[英]execfile issue with the filename argument in python

我有兩個python腳本。 一個匹配腳本,另一個是正則表達式腳本。 我想從我的matchParser.py腳本開始,然后轉到regexParser.py。 我希望我的regexParser知道matchParser的文件名並繼續使用。 我希望我能清楚解釋。 我做了很多嘗試,但不幸的是沒有成功。

我的OutputError: TypeError: coercing to Unicode: need string or buffer, file found

matchParser.py

import glob

intfiles = glob.glob("C:\Users\x\Desktop\x\*.csv")

header_saved = False
with open('outputdre121.csv','wb') as fout:
    for filename in intfiles:
        with open(filename) as fin:
            header = next(fin)
            if not header_saved:
                fout.write(header)
                header_saved = True
            for line in fin:
                fout.write(line)
print "start next script: regexParser.py"
execfile("regexParser.py")

regexParser.py

import re
import matchParser

lines = [line.strip() for line in open(matchParser.fout)] ## here the filename from MatchParser.py

with open('outputregexdre13.csv', "w") as output_csv:
    for result in lines:
        match = re.search(r'((\s\d+?[1-9])!?[ ])', result)
        if match: output_csv.write(match.group(0) + '\n')

謝謝!

我找到了解決方案:實際上非常簡單..

我將.name方法添加到matchParser.fout

lines = [line.strip() for line in open(matchParser.fout.name)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM