def CsvImport(units, unitList): # if len(sys.argv) < 3: # if errorchecking is not done here, the errors aren't # return usageError(sys.argv[0]) # handled when it is used as a module # if (sys.argv[1].find('.csv') != -1): # find returns -1 if string NOT found, -1 == bool True # units = sys.argv[1] # main csv file to modify # unitList = sys.argv[2:] # List of csv record file(s) used to update main csv # else: # return usageError(sys.argv[0]) ModFile = file(units, "r") # open file for read, units.csv Lines = ModFile.readlines() # Read List of all Lines into memory ModFile.close() key = csv.semiColonSeparatedList( Lines[0], ',') # string into list for the first line Keys for unit in unitList: try: # Catch file IOError, if the <unitname>.csv doesn't exist Unit = unit + ".csv" UnitFile = file(Unit, "r") # open csv <unitname> for read except IOError: fileError(Unit) break for numline in range( 0, len(Lines) ): # have to index the line to change Lines[numline], not line in Lines that exists only in this block line = Lines[numline] if (csv.earlyStrCmp( line, unit)): # look for unit key in the line and if found line = csv.writeList(reorderList(key, ProcessUnit(UnitFile))) UnitFile.close() Lines[numline] = line # We also need to check that the unit record being modified didn't change the key. If it did, # We will need to rename the <unitname>.csv to reflect this, so it remains usable by csvimport later. if not csv.earlyStrCmp(line, unit): # key for record was changed NewName = line[:line.find("," )] + ".csv" # save the new name. os.rename(Unit, NewName) OutFile = file( units, "w") # All changes have been made, time to write out the file for line in Lines: OutFile.write(line) OutFile.close() return
def ProcessUnit(f): l=[] m=[] line=f.readline() while (line!=""): x=csv.semiColonSeparatedList(line,',') if (len(x)>=1): m.append(removeParen(x[0])) if (len(x)>=2): if (len(x)==2): l.append(x[1]) elif (x[1].find('{')!=-1): l.append(CollapseList(x[1:])) else: l.append(CollapseStruct(x[2:])) else: l.append('') line=f.readline() return (m,l)
def CsvImport(units, unitList): # if len(sys.argv) < 3: # if errorchecking is not done here, the errors aren't # return usageError(sys.argv[0]) # handled when it is used as a module # if (sys.argv[1].find('.csv') != -1): # find returns -1 if string NOT found, -1 == bool True # units = sys.argv[1] # main csv file to modify # unitList = sys.argv[2:] # List of csv record file(s) used to update main csv # else: # return usageError(sys.argv[0]) ModFile = file(units, "r") # open file for read, units.csv Lines = ModFile.readlines() # Read List of all Lines into memory ModFile.close() key = csv.semiColonSeparatedList(Lines[0],',') # string into list for the first line Keys for unit in unitList: try: # Catch file IOError, if the <unitname>.csv doesn't exist Unit = unit+".csv" UnitFile=file(Unit, "r") # open csv <unitname> for read except IOError: fileError(Unit) break for numline in range(0,len(Lines)): # have to index the line to change Lines[numline], not line in Lines that exists only in this block line = Lines[numline] if (csv.earlyStrCmp(line,unit)): # look for unit key in the line and if found line = csv.writeList(reorderList(key,ProcessUnit(UnitFile))) UnitFile.close() Lines[numline] = line # We also need to check that the unit record being modified didn't change the key. If it did, # We will need to rename the <unitname>.csv to reflect this, so it remains usable by csvimport later. if not csv.earlyStrCmp(line, unit): # key for record was changed NewName = line[:line.find(",")]+".csv" # save the new name. os.rename(Unit, NewName) OutFile = file(units, "w") # All changes have been made, time to write out the file for line in Lines: OutFile.write(line) OutFile.close() return
def CsvExport(path, args): global showunits, ToScreen showunits = 0 ToScreen = 0 # if len(sys.argv) < 3: # return usageError(sys.argv[0]) # if (sys.argv[1].find('.csv') != -1): # find returns -1 if string NOT found, -1 == bool True # path=sys.argv[1] # args=sys.argv[2:] # else: # return usageError(sys.argv[0]) for i in args: # easy to add OPTIONS, if you do, please add to USAGE information as well if (i == "-units"): showunits = 1 if (i == "-toscreen"): ToScreen = 1 workfile = file(path, "r") KeyList = csv.semiColonSeparatedList(workfile.readline().strip()) GuideList = csv.semiColonSeparatedList(workfile.readline().strip()) Lines = workfile.readlines() workfile.close() KeyLL = len(KeyList) GuideLL = len(GuideList) numLine = 2 # we already read 2 lines from the file for line in Lines: numLine += 1 for arg in args: if (csv.earlyStrCmp(line, arg)): # Used to look up unique key entry. # It isn't strictly necessary for looking up Ex, llama.blank # But makes looking up llama, find only the milspec version line = line.strip() row = csv.semiColonSeparatedList(line) rowLL = len(row) Entry = row[0] #if (rowLL!=KeyLL or rowLL!=GuideLL): # print "Mismatch in row length", rowLL, "for", row[0], "with key len =", KeyLL, "and guide len =", GuideLL # print "Contains only", rowLL, "fields" # break # The above block should work, rarely, to catch errors... and next line should be elif rowLL: # It fails miserably because most lines in units.csv do not have the same number of fields. #if rowLL: # bool False, if row list length is zero... an empty line in file, this suppresses blank line results. print("Line:", numLine, "Columns:", rowLL, "Entry:", Entry) if ToScreen: for i in range(rowLL): if GuideList[i].find('{') != -1: print( csv.writeList( [KeyList[i]] + ProcessList(GuideList[i], row[i]))) elif GuideList[i].find(';') != -1: print( csv.writeList( [KeyList[i]] + ProcessStruct(GuideList[i], row[i]))) else: print( csv.writeList([ makeName(KeyList[i], GuideList[i]), row[i] ])) else: outfile = file(Entry + ".csv", "w") for i in range(rowLL): if GuideList[i].find('{') != -1: outfile.write( csv.writeList( [KeyList[i]] + ProcessList(GuideList[i], row[i]))) elif GuideList[i].find(';') != -1: outfile.write( csv.writeList( [KeyList[i]] + ProcessStruct(GuideList[i], row[i]))) else: outfile.write( csv.writeList([ makeName(KeyList[i], GuideList[i]), row[i] ]))
lis = f.readlines() olist = [] # lis.sort() for l in lis: if (len(olist) != 0): if (l == olist[len(olist) - 1]): continue olist = olist + [l] f.close() # f = open (arg,"w") # f.writelines(olist); # f.close(); linkfil = open(linknam) link = linkfil.readlines() for l in link: m = csv.semiColonSeparatedList(l, ";") if not m[0] in jumps: jumps[m[0]] = [] if not m[2] in jumps: jumps[m[2]] = [] if (not m[2] in jumps[m[0]]): jumps[m[0]].append(m[2]) if (not m[0] in jumps[m[2]]): jumps[m[2]].append(m[0]) #now to read this sucker for i in range(len(olist)): olist[i] = csv.semiColonSeparatedList(olist[i], ';') tab = csv.makeTable(olist) secs = {} for i in tab: sys = tab[i]
retstring+=printexplosion(explosion) retstring+=',' #equipment space retstring+="0" #Dummy value retstring+=',' #num animation points retstring+="0" #write to file retstring+="\n" o=file("units.bak.csv","w"); where=retstring.find(",") unitname=retstring[0:where+1] masterunitfile=file("units.csv","r") line=masterunitfile.readline() import csv externalkeys=csv.semiColonSeparatedList(line,","); foundme=0 usedlist={} if usecoords!=0: usedlist=coordkeys while (line!=""): if (prefixCmp(unitname,line)): o.write(csv.writeList(fixupKeys(externalkeys, csv.semiColonSeparatedList(line,","), internalkeys, csv.semiColonSeparatedList(retstring,","), usedlist))); foundme=1 else: o.write(line); line=masterunitfile.readline();
def CsvExport(path, args): global showunits, ToScreen showunits=0 ToScreen=0 # if len(sys.argv) < 3: # return usageError(sys.argv[0]) # if (sys.argv[1].find('.csv') != -1): # find returns -1 if string NOT found, -1 == bool True # path=sys.argv[1] # args=sys.argv[2:] # else: # return usageError(sys.argv[0]) for i in args: # easy to add OPTIONS, if you do, please add to USAGE information as well if (i=="-units"): showunits=1 if (i=="-toscreen"): ToScreen=1 workfile = file(path, "r") KeyList = csv.semiColonSeparatedList(workfile.readline().strip()) GuideList = csv.semiColonSeparatedList(workfile.readline().strip()) Lines = workfile.readlines() workfile.close() KeyLL = len(KeyList) GuideLL = len(GuideList) numLine = 2 # we already read 2 lines from the file for line in Lines: numLine += 1 for arg in args: if (csv.earlyStrCmp(line,arg)): # Used to look up unique key entry. # It isn't strictly necessary for looking up Ex, llama.blank # But makes looking up llama, find only the milspec version line = line.strip() row = csv.semiColonSeparatedList(line) rowLL = len(row) Entry = row[0] #if (rowLL!=KeyLL or rowLL!=GuideLL): # print "Mismatch in row length", rowLL, "for", row[0], "with key len =", KeyLL, "and guide len =", GuideLL # print "Contains only", rowLL, "fields" # break # The above block should work, rarely, to catch errors... and next line should be elif rowLL: # It fails miserably because most lines in units.csv do not have the same number of fields. #if rowLL: # bool False, if row list length is zero... an empty line in file, this suppresses blank line results. print("Line:", numLine, "Columns:", rowLL, "Entry:", Entry) if ToScreen: for i in range(rowLL): if GuideList[i].find('{')!=-1: print(csv.writeList([KeyList[i]]+ProcessList(GuideList[i],row[i]))) elif GuideList[i].find(';')!=-1: print(csv.writeList([KeyList[i]]+ProcessStruct(GuideList[i],row[i]))) else: print(csv.writeList([makeName(KeyList[i],GuideList[i]), row[i]])) else: outfile = file(Entry+".csv", "w") for i in range(rowLL): if GuideList[i].find('{')!=-1: outfile.write(csv.writeList([KeyList[i]]+ProcessList(GuideList[i],row[i]))) elif GuideList[i].find(';')!=-1: outfile.write(csv.writeList([KeyList[i]]+ProcessStruct(GuideList[i],row[i]))) else: outfile.write(csv.writeList([makeName(KeyList[i],GuideList[i]), row[i]]))
lis = f.readlines(); olist=[] # lis.sort() for l in lis: if (len(olist)!=0): if (l==olist[len(olist)-1]): continue olist=olist+[l]; f.close() # f = open (arg,"w") # f.writelines(olist); # f.close(); linkfil=open(linknam) link=linkfil.readlines(); for l in link: m=csv.semiColonSeparatedList(l,";") if not m[0] in jumps: jumps[m[0]]=[] if not m[2] in jumps: jumps[m[2]]=[] if (not m[2] in jumps[m[0]]): jumps[m[0]].append(m[2]) if (not m[0] in jumps[m[2]]): jumps[m[2]].append(m[0]) #now to read this sucker for i in range(len(olist)): olist[i]=csv.semiColonSeparatedList(olist[i],';') tab = csv.makeTable(olist ); secs={} for i in tab: sys=tab[i]
retstring += printexplosion(explosion) retstring += ',' #equipment space retstring += "0" #Dummy value retstring += ',' #num animation points retstring += "0" #write to file retstring += "\n" o = file("units.bak.csv", "w") where = retstring.find(",") unitname = retstring[0:where + 1] masterunitfile = file("units.csv", "r") line = masterunitfile.readline() import csv externalkeys = csv.semiColonSeparatedList(line, ",") foundme = 0 usedlist = {} if usecoords != 0: usedlist = coordkeys while (line != ""): if (prefixCmp(unitname, line)): o.write( csv.writeList( fixupKeys(externalkeys, csv.semiColonSeparatedList(line, ","), internalkeys, csv.semiColonSeparatedList(retstring, ","), usedlist))) foundme = 1 else:
retstring += "," # equipment space retstring += "0" # Dummy value retstring += "," # num animation points retstring += "0" # write to file retstring += "\n" o = file("units.bak.csv", "w") where = retstring.find(",") unitname = retstring[0 : where + 1] masterunitfile = file("units.csv", "r") line = masterunitfile.readline() import csv externalkeys = csv.semiColonSeparatedList(line, ",") foundme = 0 usedlist = {} if usecoords != 0: usedlist = coordkeys while line != "": if prefixCmp(unitname, line): o.write( csv.writeList( fixupKeys( externalkeys, csv.semiColonSeparatedList(line, ","), internalkeys, csv.semiColonSeparatedList(retstring, ","), usedlist, )