import platform import json from io import StringIO #Globals currOS = platform.platform() if currOS.find('Windows') == 0: LOCALBASE = 'D:/website/' #LOCALBASE = 'A:/test/' else: LOCALBASE = '/mnt/d/website/' NAMEFILE = LOCALBASE + "names.tsv" OUTFILE = LOCALBASE + "names.json" ERRFILE = LOCALBASE + "namerr.txt" flErr = open(ERRFILE, "w", encoding='latin1') def get_name_list(flNames): nameList = [] stName = flNames.read() lsIn = stName.split('\n') n = 0 for line in lsIn: n += 1 #get rid of leading tabs line = line.strip() # ignore blank lines if line == "": continue # or commented out lines if line[0] == "#": continue lsRec = line.split('\t') numFields = len(lsRec) # Make sure we have all fields filled, at least with blanks while numFields < 6: lsRec.append("") numFields += 1 nameList.append(lsRec) return nameList def get_names(nameList): liOut = [] for entry in nameList: diOut = {} #5th entry is a |-separated list of countries stRes = entry[4] liRes = stRes.split('|') if liRes == [""]: liRes = [] #6th entry is a |-separated list of names stAlt = entry[5] liAlt = stAlt.split('|') if liAlt == [""]: liAlt = [] diOut["last_name"] = entry[0] diOut["first_name"] = entry[1] diOut["dates"] = entry[2] diOut["birth_country"] = entry[3] diOut["residences"] = liRes diOut["alt_names"] = liAlt liOut.append(diOut) return(liOut) def main(): flNames = open(NAMEFILE, "r", encoding='latin1') if not flNames: stErr = "Cannot read ", NAMEFILE flErr.write(stErr) return False nameList = get_name_list(flNames) liNames = get_names(nameList) stOut = json.dumps(liNames, indent=4) print(stOut) # end main if __name__ == "__main__": main()