Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- encoding: utf-8 -*-
- # Author: Daniel de Faria Godoi
- # Email: danielfgodoi@gmail.com
- import sys
- import os
- import re
- import csv
- import repr as reprlib
- import locale
- from Tkinter import *
- from tkFileDialog import *
- class Object(object):
- def __init__(self, fileList, targetFileName):
- self.fileList = fileList
- self.targetFileName = targetFileName
- rows = []
- header = ["File/Cell Value", "0", "1", "2", "3", "4", "5", "6"]
- for i in range(len(fileList)):
- with open(fileList[i], "r") as f:
- # Remove HTML tags
- content = f.read().replace("Band", "").replace("Number", "").replace("Cell", "").replace("Value", "").replace("Count", "")
- regex = re.compile('<.*?>')
- data = re.sub(regex, '', content)
- # Remove '.html' from file name
- row = [fileList[i].replace(".html", "").rsplit('/', 1)[1]]
- current = 0
- last = 0
- values = [0, 0, 0, 0, 0, 0, 0]
- for value in data.split():
- current += 1
- if current == 2 or (current + 1) % 3 == 0:
- last = int(float(value))
- if current % 3 == 0:
- # print last, value
- try:
- if value == "None":
- value = 0
- values[last] = value
- # row.append(locale.format("%f", float(value)))
- except ValueError:
- pass
- # print values
- for item in values:
- row.append(item)
- rows.append(row)
- decimalPoint = locale.localeconv()['decimal_point']
- if decimalPoint == ",":
- delimiterPerLocale = ";"
- else:
- delimiterPerLocale = ","
- if targetFileName:
- with open(targetFileName, "wb") as targetFileName:
- writer = csv.writer(targetFileName, delimiter = delimiterPerLocale)
- writer.writerow(header)
- for j in range(len(rows)):
- writer.writerow(rows[j])
- if(targetFileName):
- print(targetFileName)
- print("Arquivo gerado com sucesso!")
- if __name__ == "__main__":
- if "Portuguese" in locale.setlocale(locale.LC_ALL, ""):
- types = [("CSV (separado por ponto e vírgula)", ".csv"), ("Todos os arquivos", "*.*")]
- else:
- types = [("CSV (comma separated)", ".csv"), ("All files", "*.*")]
- # Hide GUI elements
- root = Tk()
- root.withdraw()
- # Get directory to work
- initDir = askdirectory()
- # initDir = "D:/Raquel/data"
- dirList = []
- # Walk through the initial dir
- for dirname, dirnames, filenames in os.walk(initDir):
- for subdirname in dirnames:
- currentDir = os.path.join(dirname, subdirname)
- fileList = []
- # For each subdir it will check if theres any HTML file
- for file in os.listdir(currentDir):
- if file.endswith(".html"):
- fileList.append(str(currentDir + "/" + file))
- # If there's any HTML file it will execute the task
- if fileList:
- # Sort file list to be nice
- fileList.sort()
- # Get the target filename to be saved (current dir with CSV extension)
- targetFileName = currentDir + ".csv"
- # Create the object to convert the files
- # convertedFile = Object(fileList, targetFileName)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement