def convert_xml_to_csv(input_file, mapping_file=None, output_csv_file_path=None, overwrite="yes"): """ It takes xml file path as input and converts to csv. Arguments: 1. input_file: Takes xml file path as input 2. mapping_file: If a mapping file path is given, it is used to map columns with the meaningful name as recognized by the user else the tags in the xml file will be used as column names in the csv file. 3. output_csv_file_path: If user gives the output_csv_file_path, creating an csv file in that path else creating csv file in the path from where he have given xml file. Returns: Returns output csv file path. """ count = 0 try: dict_response = xml_Utils.convert_xml_to_list_of_dict(input_file) if mapping_file: mapping_dict = data_Utils.get_credentials(mapping_file, 'mapping_scheme') mapping_dictionary = {v: k for k, v in mapping_dict.items()} else: mapping_dictionary = {} if output_csv_file_path: output_csv_file = output_csv_file_path else: output_csv_file = input_file.replace(".xml", ".csv") if overwrite == "no": output_csv_file = file_Utils.addTimeDate(output_csv_file) f = open(output_csv_file, 'wb+') csvwriter = csv.writer(f) for element in dict_response: if count == 0: header = list(element.keys()) for index, val in enumerate(header): for key, value in mapping_dictionary.items(): if val == value: header[index] = key csvwriter.writerow(header) count += 1 csvwriter.writerow(list(element.values())) f.close() except Exception as exception: print_exception(exception) output_csv_file = None return output_csv_file
def get_execution_files(filepath, execution_dir, extn): """Get the execution files like resultfile, logfile etc""" filename = file_Utils.getFileName(filepath) nameonly = file_Utils.getNameOnly(filename) if extn.lower() == "res": fullpath = execution_dir + os.sep + nameonly + "_results" + "." + extn else: fullpath = execution_dir + os.sep + nameonly + '.' + extn if file_Utils.fileExists(fullpath): fullpath = file_Utils.addTimeDate(fullpath) return fullpath
def convert_csv_or_excel_to_xml(input_file, mapping_file=None, output_xml_file_path=None, overwrite="yes"): """ Takes file path as input 1. If it is excel file, converts to csv and then converts csv file to xml 2. If it is csv file, converts to xml file. 3. Mapping file is used to map the column names in the excel sheet to a meaningful name as recognized by the code. Arguments: 1. input_file: input_file which is either csv file path or excel file path 2. mapping_file: If a mapping file path is given, it is used to map columns with the meaningful name as recognized by the user else the spaces in the column names will be replaced by "_" in the output xml 3. output_xml_file_path: If user gives the output_xml_file_path, creating an xml file in that path else creating xml file in the path from where he have given csv or excel file. Returns: 1. output_xml_file_path: Returns the output xml file path 2 output_dict: Updates the output_dict with json string and with output xml. """ output_dict = {} generate_csv = False try: if ".xls" in input_file: input_file = convert_excel_to_csv(input_file) generate_csv = True dict_response = convert_csv_to_list_of_dict(input_file) json_response = json.dumps(dict_response, sort_keys=False, indent=4, separators=(',', ': '), encoding="utf-8") if mapping_file: mapping_dict = data_Utils.get_credentials(mapping_file, 'mapping_scheme') mapping_dictionary = {v: k for k, v in mapping_dict.items()} else: mapping_dictionary = {} result = [] result.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<systems>\n") for i in range(len(dict_response)): result.append(" <system name=" + "\"" + str(i + 1) + "\">\n") for key, value in dict_response[i].items(): if mapping_dictionary: if key in mapping_dictionary and mapping_dictionary[key]: result.append((" <{0}>{1}</{0}>\n").format( mapping_dictionary[key], value)) else: result.append( (" <{0}>{1}</{0}>\n").format(key, value)) else: result.append((" <{0}>{1}</{0}>\n").format( "_".join(key.split()), value)) result.append(" </system>\n") result.append("</systems>") xml_res = ''.join(result) if type(input_file) == file: input_file = input_file.name if generate_csv: os.remove(input_file) output_dict["{0}_json_response".format(input_file.replace( ".csv", ''))] = json_response output_dict["{0}_xml_response".format(input_file.replace( ".csv", ''))] = xml_res if output_xml_file_path: output_xml_file = output_xml_file_path else: output_xml_file = input_file.replace(".csv", ".xml") if overwrite == "no": output_xml_file = file_Utils.addTimeDate(output_xml_file) f = open(output_xml_file, "wb+") f.write(xml_res) f.close() except Exception as exception: print_exception(exception) output_xml_file = None return output_xml_file, output_dict