예제 #1
0
파일: csvimport.py 프로젝트: DMJC/vsengine
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
예제 #2
0
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
예제 #3
0
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]
                                ]))
예제 #4
0
 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();
 if (foundme==0):
     o.write(csv.writeList(fixupKeys(externalkeys,
                                     [],
                                     internalkeys,
                                     csv.semiColonSeparatedList(retstring,","))));
 masterunitfile.close()
 o.close()
 import os
 os.rename("units.bak.csv","units.csv");
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]]))
예제 #6
0
 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()
 if (foundme == 0):
     o.write(
         csv.writeList(
             fixupKeys(externalkeys, [], internalkeys,
                       csv.semiColonSeparatedList(retstring, ","))))
 masterunitfile.close()
 o.close()
 import os
 os.rename("units.bak.csv", "units.csv")