python中带有filename参数的execfile问题

2024-10-01 15:47:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个python脚本。一个匹配脚本,另一个是regex脚本。我想从matchParser.py脚本开始,然后转到regexParser.py。我希望我的regexParser知道matchParser的文件名并继续使用。我希望我能解释清楚。我尝试了很多,但不幸没有成功

我的输出者: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')

谢谢


Tags: csvinpyimport脚本forwithline

热门问题