Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from pathlib import Path
- folders = ["reports_to_convert/", "converted_reports/", "report_archive/"]
- for folder in folders:
- if not os.path.exists(folder):
- print('A required directory is missing.')
- try:
- print('Creating directory...')
- os.mkdir(folder)
- except OSError:
- print("Creation of the directory %s failed" % folder)
- else:
- print("Successfully created the directory %s " % folder)
- input_folder = Path("reports_to_convert/")
- output_folder = Path("converted_reports/")
- archive_folder = Path("report_archive/")
- # count number of commas in a string
- def count_commas(string):
- count = 0
- for character in string:
- if character == ',':
- count += 1
- return count
- # returns the value of column x for a row string
- def column(x, string):
- column_list = string.split(",")
- #print(column_list)
- return column_list[x-1]
- # delete the xth comma in a string
- def delete_comma(x, string):
- comma_num = 0
- i = 0
- for character in string:
- if character == ',':
- comma_num += 1
- if comma_num == x:
- new_string = string[:i] + string[i+1:]
- return new_string
- i += 1
- return None
- # adds a comma to strings that do not end in a comma
- def add_ending_comma(string):
- if string.rstrip()[-1] != ',':
- string = string.rstrip() + ',' + "\n"
- return string
- # removes trailing commas from a string
- def rmv_tr_commas(raw_line):
- #print('Removing trailing commas...')
- try:
- line = raw_line.rstrip()
- i = len(line) - 1
- while line[i - 1] == ',':
- i -= 1
- return line[:i+1]
- except:
- print('Failed to remove trailing commas.')
- # takes file rows as list and removes any elements containing the word "Invoice" and the following 5 elements
- def invoice_remove(lst):
- #print('Removing Invoice rows...')
- try:
- for i in range(len(lst)-1, -1, -1):
- if 'Invoice' in lst[i]:
- for j in range(6):
- lst.pop(i+j)
- i -= 1
- return lst
- except:
- print('Failed to remove Invoice rows.')
- # for each line starting with BSCA, combine the next two lines into BSCA line and remove lines with extra commas
- def concat_lines(lst):
- #print('Concatenating lines...')
- try:
- for i in range(len(lst)-1, -1, -1):
- if 'BS' in lst[i]:
- lst[i] = add_ending_comma(lst[i])
- for j in range(2):
- lst[i] = rmv_tr_commas(lst[i])
- lst[i] += lst[i+1]
- lst.pop(i+1)
- ## try:
- ## if count_commas(lst[i]) > 34:
- ## lst[i] = delete_comma(10, lst[i])
- ## after_reductions = float(column(10, lst[i])) - float(column(20, lst[i]))
- ## if after_reductions >= 1000:
- ## lst[i] = delete_comma(21, lst[i])
- ## except ValueError:
- ## print("BAD")
- return(lst)
- except:
- print('Failed to concatenate lines.')
- # write each element of lst to a line in a new csv file
- def write_lines(lst, file):
- #print('Writing to file...')
- try:
- with open(output_folder / file, "w") as f:
- for line in lst:
- f.write(line)
- print(file + ' has been created.')
- except:
- print(file + ' COULD NOT BE CREATED.')
- if __name__ == "__main__":
- converted = False
- if os.listdir(input_folder) == []:
- print('There are no reports to convert. Place reports in the reports_to_convert folder.')
- else:
- try:
- ## print('Scanning reports_to_convert...')
- for file in os.listdir(input_folder):
- if file.endswith(".csv"):
- #print(file + ' found.')
- with open(input_folder / file, 'r') as f:
- #print(file + ' opened. Converting...')
- lst = f.readlines()
- write_lines(concat_lines(invoice_remove(lst)), file[:-4]+' - CONVERTED.csv')
- os.replace(input_folder / file, archive_folder / file)
- converted = True
- if converted:
- print('-ALL REPORTS SUCESSFULLY CONVERTED-.')
- else:
- print('No valid .csv files found in reports_to_convert.')
- except:
- print('*THERE WAS A PROBLEM CONVERTING THE REPORTS*')
- input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement