python - 在 python 中从文本文件(也特定于路径)中附加带有特定单词(RegEx)的 CSV
问题描述
# coding=utf-8
# Libreria RegEx de Python.
import re
# Libreria para rutas.
import os
import csv
# function scan folder DiarioOficial
def scan_folder():
# directory 'path'
path = '/Users/.../DiarioOficial'
# contador de ficheros del path
count = 0
# variables declaration
company_name = ''
company_capital = ''
# creation csv as csvFile
with open('aaa.csv', 'a') as csvFile:
# iterate all paths in the folder DiarioOficial without name
for (path, dirnames, file_names) in os.walk(path):
# iterate over all the files in the path (+ file_name)
for file_name in file_names:
# Add extension that is required
if file_name.endswith(".txt"):
# summatory count files in DiarioOficial folder
count = count + 1
# concatenation path + file name
file_path=os.path.join(path, file_name)
#print(file_path)
# open and read the file path
mensaje = open(file_path).read()
# Replace a newline for a space
mensaje = mensaje.replace("\n","")
# Company Name
keywords_cap = ['SpA', 'SPA', 'LIMITADA', 'LTDA', 'S.A.', 'E.I.R.L.']
keywords_cap = map(re.escape, keywords_cap)
keywords_cap.sort(key=len, reverse=True)
obj = re.compile(r'[:,;.]\s*"?([^:,;.]*?(?<!\w)(?:{}))'.format('|'.join(keywords_cap)))
if obj:
company_name = obj.search(mensaje)
else:
company_name = "None"
# Social Capital ($)
cap = r"\s*(CAPITAL:\s+([^-]*)|Capital social:\s+([^-]*)|Capital:\s+([^-]*))"
caps = re.search(cap, mensaje)
if caps:
company_capital = caps.group()
else:
company_capital = 'None'
print (count)
csvData = [company_name, company_capital]
writer = csv.writer(csvFile, delimiter='-')
headers = ['COMPANY NAME', 'COMPANY CAPITAL']
writer.writerow(headers) # print the header row
writer.writerow(csvData) # print the Data in csv
scan_folder()
我想为信息在路径/Users/.../DiarioOficial 中的公司创建一个带有行的csv。此行包括公司名称和公司资本。但是Python:
错误:
回溯(最后一次调用):文件“/Users/anna/PycharmProjects/extractData/post.py”,第 61 行,在 scan_folder() 文件“/Users/anna/PycharmProjects/extractData/post.py”,第 59 行, in scan_folder writer.writerow(csvData) # print the Data in csv 247 ValueError: I/O operation on closed file
解决方案
推荐阅读
- linux-kernel - 处理器间通信驱动,用户空间交互
- python - Python:设施位置问题->如何获得每个设施的总成本和总客户数
- typescript - Switch in TypeScript does not narrow actions
- javascript - 选项卡未在 HTMLButtonElement.onclick 中定义
- javascript - Chrome 上的 Serviceworker 不显示 HTTP 基本身份验证对话框?
- javascript - 当 opacity:0 或 visibility:hidden 时,不应用类
- .net-core - 在 AceClient 中使用网络 IP 地址作为数据源时出错
- java - 如何使用模型和适配器类在折线图中传递值
- javascript - 为什么 Number.parseInt('111AAA') 像它在 JS 中那样工作?这种行为背后的原因是什么?
- java - 如何使用php api在c面板中使用另一个名称复制现有目录