Advertisement
Guest User

Untitled

a guest
May 21st, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. # -*- encoding: utf-8 -*-
  2. # Author: Daniel de Faria Godoi
  3. # Email: danielfgodoi@gmail.com
  4.  
  5. import sys
  6. import os
  7. import re
  8. import csv
  9. import repr as reprlib
  10. import locale
  11. from Tkinter import *
  12. from tkFileDialog import *
  13.  
  14. class Object(object):
  15.  
  16. def __init__(self, fileList, targetFileName):
  17. self.fileList = fileList
  18. self.targetFileName = targetFileName
  19. rows = []
  20.  
  21. header = ["File/Cell Value", "0", "1", "2", "3", "4", "5", "6"]
  22.  
  23. for i in range(len(fileList)):
  24. with open(fileList[i], "r") as f:
  25. # Remove HTML tags
  26. content = f.read().replace("Band", "").replace("Number", "").replace("Cell", "").replace("Value", "").replace("Count", "")
  27. regex = re.compile('<.*?>')
  28. data = re.sub(regex, '', content)
  29.  
  30. # Remove '.html' from file name
  31. row = [fileList[i].replace(".html", "").rsplit('/', 1)[1]]
  32.  
  33. current = 0
  34. last = 0
  35. values = [0, 0, 0, 0, 0, 0, 0]
  36.  
  37. for value in data.split():
  38. current += 1
  39.  
  40. if current == 2 or (current + 1) % 3 == 0:
  41. last = int(float(value))
  42.  
  43. if current % 3 == 0:
  44. # print last, value
  45.  
  46. try:
  47. if value == "None":
  48. value = 0
  49.  
  50. values[last] = value
  51. # row.append(locale.format("%f", float(value)))
  52. except ValueError:
  53. pass
  54.  
  55. # print values
  56.  
  57. for item in values:
  58. row.append(item)
  59.  
  60. rows.append(row)
  61.  
  62. decimalPoint = locale.localeconv()['decimal_point']
  63. if decimalPoint == ",":
  64. delimiterPerLocale = ";"
  65. else:
  66. delimiterPerLocale = ","
  67.  
  68. if targetFileName:
  69. with open(targetFileName, "wb") as targetFileName:
  70. writer = csv.writer(targetFileName, delimiter = delimiterPerLocale)
  71. writer.writerow(header)
  72. for j in range(len(rows)):
  73. writer.writerow(rows[j])
  74.  
  75. if(targetFileName):
  76. print(targetFileName)
  77. print("Arquivo gerado com sucesso!")
  78.  
  79.  
  80. if __name__ == "__main__":
  81. if "Portuguese" in locale.setlocale(locale.LC_ALL, ""):
  82. types = [("CSV (separado por ponto e vírgula)", ".csv"), ("Todos os arquivos", "*.*")]
  83.  
  84. else:
  85. types = [("CSV (comma separated)", ".csv"), ("All files", "*.*")]
  86.  
  87. # Hide GUI elements
  88. root = Tk()
  89. root.withdraw()
  90.  
  91. # Get directory to work
  92. initDir = askdirectory()
  93. # initDir = "D:/Raquel/data"
  94. dirList = []
  95.  
  96. # Walk through the initial dir
  97. for dirname, dirnames, filenames in os.walk(initDir):
  98. for subdirname in dirnames:
  99. currentDir = os.path.join(dirname, subdirname)
  100.  
  101. fileList = []
  102.  
  103. # For each subdir it will check if theres any HTML file
  104. for file in os.listdir(currentDir):
  105. if file.endswith(".html"):
  106. fileList.append(str(currentDir + "/" + file))
  107.  
  108. # If there's any HTML file it will execute the task
  109. if fileList:
  110. # Sort file list to be nice
  111. fileList.sort()
  112.  
  113. # Get the target filename to be saved (current dir with CSV extension)
  114. targetFileName = currentDir + ".csv"
  115.  
  116. # Create the object to convert the files
  117. # convertedFile = Object(fileList, targetFileName)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement