SHARE
TWEET

Untitled

a guest Feb 14th, 2020 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. from pathlib import Path
  3.  
  4. folders = ["reports_to_convert/", "converted_reports/", "report_archive/"]
  5. for folder in folders:
  6.     if not os.path.exists(folder):
  7.         print('A required directory is missing.')
  8.         try:
  9.             print('Creating directory...')
  10.             os.mkdir(folder)
  11.         except OSError:
  12.             print("Creation of the directory %s failed" % folder)
  13.         else:
  14.             print("Successfully created the directory %s " % folder)
  15.  
  16. input_folder = Path("reports_to_convert/")
  17. output_folder = Path("converted_reports/")
  18. archive_folder = Path("report_archive/")
  19.  
  20. # count number of commas in a string
  21. def count_commas(string):
  22.     count = 0
  23.     for character in string:
  24.         if character == ',':
  25.             count += 1
  26.     return count
  27.  
  28. # returns the value of column x for a row string
  29. def column(x, string):
  30.     column_list = string.split(",")
  31.     #print(column_list)
  32.     return column_list[x-1]
  33.  
  34. # delete the xth comma in a string
  35. def delete_comma(x, string):
  36.     comma_num = 0
  37.     i = 0
  38.     for character in string:
  39.         if character == ',':
  40.             comma_num += 1
  41.         if comma_num == x:
  42.             new_string = string[:i] + string[i+1:]
  43.             return new_string
  44.         i += 1
  45.     return None
  46.  
  47. # adds a comma to strings that do not end in a comma
  48. def add_ending_comma(string):
  49.     if string.rstrip()[-1] != ',':
  50.         string = string.rstrip() + ',' + "\n"
  51.     return string
  52.  
  53. # removes trailing commas from a string
  54. def rmv_tr_commas(raw_line):
  55.     #print('Removing trailing commas...')
  56.     try:
  57.         line = raw_line.rstrip()
  58.         i = len(line) - 1
  59.         while line[i - 1] == ',':
  60.             i -= 1
  61.         return line[:i+1]
  62.     except:
  63.         print('Failed to remove trailing commas.')
  64.        
  65.  
  66. # takes file rows as list and removes any elements containing the word "Invoice" and the following 5 elements
  67. def invoice_remove(lst):
  68.     #print('Removing Invoice rows...')
  69.     try:
  70.         for i in range(len(lst)-1, -1, -1):
  71.             if 'Invoice' in lst[i]:
  72.                 for j in range(6):
  73.                     lst.pop(i+j)
  74.                     i -= 1
  75.         return lst
  76.     except:
  77.         print('Failed to remove Invoice rows.')
  78.  
  79. # for each line starting with BSCA, combine the next two lines into BSCA line and remove lines with extra commas
  80. def concat_lines(lst):
  81.     #print('Concatenating lines...')
  82.     try:
  83.         for i in range(len(lst)-1, -1, -1):
  84.             if 'BS' in lst[i]:
  85.                 lst[i] = add_ending_comma(lst[i])
  86.                 for j in range(2):
  87.                     lst[i] = rmv_tr_commas(lst[i])
  88.                     lst[i] += lst[i+1]
  89.                     lst.pop(i+1)
  90. ##                try:
  91. ##                    if count_commas(lst[i]) > 34:
  92. ##                        lst[i] = delete_comma(10, lst[i])
  93. ##                        after_reductions = float(column(10, lst[i])) - float(column(20, lst[i]))
  94. ##                        if after_reductions >= 1000:
  95. ##                            lst[i] = delete_comma(21, lst[i])
  96. ##                except ValueError:
  97. ##                    print("BAD")
  98.         return(lst)
  99.     except:
  100.         print('Failed to concatenate lines.')
  101.  
  102. # write each element of lst to a line in a new csv file
  103. def write_lines(lst, file):
  104.     #print('Writing to file...')
  105.     try:
  106.         with open(output_folder / file, "w") as f:
  107.             for line in lst:
  108.                 f.write(line)
  109.         print(file + ' has been created.')
  110.     except:
  111.         print(file + ' COULD NOT BE CREATED.')
  112.  
  113. if __name__ == "__main__":
  114.     converted = False
  115.     if os.listdir(input_folder) == []:
  116.         print('There are no reports to convert. Place reports in the reports_to_convert folder.')
  117.     else:
  118.         try:
  119. ##        print('Scanning reports_to_convert...')
  120.             for file in os.listdir(input_folder):
  121.                 if file.endswith(".csv"):
  122.                     #print(file + ' found.')
  123.                     with open(input_folder / file, 'r') as f:
  124.                         #print(file + ' opened. Converting...')
  125.                         lst = f.readlines()
  126.                         write_lines(concat_lines(invoice_remove(lst)), file[:-4]+' - CONVERTED.csv')
  127.                     os.replace(input_folder / file, archive_folder / file)
  128.                     converted = True
  129.             if converted:
  130.                 print('-ALL REPORTS SUCESSFULLY CONVERTED-.')
  131.             else:
  132.                 print('No valid .csv files found in reports_to_convert.')
  133.         except:
  134.             print('*THERE WAS A PROBLEM CONVERTING THE REPORTS*')
  135.     input()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top