python

时间:2017-01-20 12:07:38

标签: python csv

我有两个python脚本。一个匹配脚本,另一个是正则表达式脚本。我想从我的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')

谢谢!

1 个答案:

答案 0 :(得分:0)

我找到了解决方案: 实际上非常简单..

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

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