Author | Message | Time |
---|---|---|
Tontow | Here is the file I'm trying to read http://www.geocities.com/tontowmerlin/programing/file.html The file contains variables that where stored by a c/c++ program. I have tried opening the file in several different modes, but all I can get out of it is garbage. Another guy has managed to read it in python using the following code: [code] from igc import * import struct class CSVEntityWriter: def write( self, f, entity, items = None, ignore = [] ): if items == None: items = entity.attribs.keys() for i in items: if entity.attribs.has_key( i ): f.write( '"' + str( entity.attribs[ i ] ) + '",' ) else: f.write( ',' ) if ignore == None: ignore = entity.attribs.keys() for name, value in entity.attribs.iteritems(): if (not name in items) and (not name in ignore): f.write( '"' + str( value ) + '",' ) f.write( "\n" ) class CSVWriter: def __init__(self, igcreader): self.reader = igcreader def write( self ): self.dir = self.reader.getCoreName() + '/' try: os.makedirs( self.dir ) except OSError, err: if err.errno != errno.EEXIST: print "Fatal Error:", err return self.writeFactions() self.writeShips() self.writeParts() self.writeDmgTable() self.writeMissiles() self.writeMines() self.writeProbes() self.writeProjectiles() self.writeStations() self.writeDrones() self.writeChaff() self.writeTech() self.writeTreasure() self.writeChaffTable() # self.writeTechTrees() def writeData( self, path, data, attribs = None, ignore = [] ): entWriter = CSVEntityWriter() print "Writing", path, len(data), "entities" if len(data) == 0: return attribs = None ignore = [] if attribs == None: attribs = data[0].attribs.keys() if ignore == None: ignore = data[0].attribs.keys() attribs.sort() f = file( self.dir + path, "w" ) for a in attribs: f.write( '"' + a + '",' ) for key in data[0].attribs: if ( not key in attribs ) and ( not key in ignore ): f.write( '"' + key + '",' ) f.write( "\n" ) for d in data: entWriter.write( f, d, attribs, ignore ) def writeShips( self ): attribs = [ "Name", "Sig", "Scan", "Ammo", "Rip Time" ] # ignore = [ "Description" ] ignore = [] self.writeData( "ships.csv", self.reader.ships, attribs, ignore ) def writeFactions( self ): attribs = [ "Name", "Ship Sensors", "Ship Signature" ] self.writeData( "factions.csv", self.reader.factions, attribs ) def writeTech( self ): attribs = [ "Name" ] ignore = [ "Description" ] self.writeData( "tech.csv", self.reader.techs, attribs, ignore ) def writeParts( self ): attribs = [ "Name" ] ignore = [ "Description", "Type" ] self.writeData( "parts.csv", self.reader.parts, attribs, ignore ) self.writeData( "weapons.csv", self.reader.weapons, attribs, ignore ) self.writeData( "shields.csv", self.reader.shields, attribs, ignore ) self.writeData( "cloaks.csv", self.reader.cloaks, attribs, ignore ) self.writeData( "boosters.csv", self.reader.boosters, attribs, ignore ) def writeMissiles( self ): attribs = [ "Name" ] ignore = [ "Description", "Unused" ] self.writeData( "missiles.csv", self.reader.missiles, attribs, ignore ) def writeMines( self ): attribs = [ "Name" ] ignore = [ "Description", "Unused" ] self.writeData( "mines.csv", self.reader.mines, attribs, ignore ) def writeDrones( self ): attribs = [ "Name" ] ignore = [ "Description", "Unused" ] self.writeData( "drones.csv", self.reader.drones, attribs, ignore ) def writeChaff( self ): attribs = [ "Name" ] ignore = [ "Description", "Unused" ] self.writeData( "chaff.csv", self.reader.chaff, attribs, ignore ) def writeProjectiles( self ): attribs = [ "UID" ] self.writeData( "projectiles.csv", self.reader.projectiles, attribs ) def writeProbes( self ): attribs = [ "Name" ] ignore = [ "Description", "Unused" ] self.writeData( "probes.csv", self.reader.probes, attribs, ignore ) def writeStations( self ): attribs = [ "Name" ] ignore = [ "Description", "Unused" ] self.writeData( "stations.csv", self.reader.stations, attribs, ignore ) def writeDmgTable( self ): if self.reader.const != None: print "Writing dmgtable.csv" f = file( self.dir + "dmgtable.csv", "w" ) f.write( '"DC \ AC","asteroid (AC0)","light (AC1)","medium (AC2)","heavy (AC3)","extra heavy (AC4)","utility (AC5)","minor base hull (AC6)","major base hull (AC7)","lt & med shield (AC8)","minor base shld (AC9)","major base shld (AC10)","parts (AC11)","lt base hull (AC12)","lt base shld (AC13)","large shield (AC14)","AC15","AC16","AC17","AC18","AC19"\n' ) for i in range(20): f.write( '"DC' + str(i) + '",' ) for j in range(20): f.write( '"' + str( self.reader.const.dmgTable[ i*20 + j ] ) + '",' ) f.write( '\n' ) def writeChaffTable( self ): print "Writing chaff table" f = file( self.dir + "chaff_table.csv", "w" ) f.write( '"Missile \ Chaff",' ) for c in self.reader.chaff: f.write( '"' + c.attribs["ld_name"] + '",' ) for m in self.reader.missiles: f.write( "\n" ) f.write( '"' + m.attribs["ld_name"] + '",' ) for c in self.reader.chaff: ecm = c.attribs["strength"] resist = m.attribs["resist"] chance = 0 if( ecm <= resist ): chance = 0.5 * ecm / resist else: chance = 1.0 - (0.5 * resist / ecm) f.write( str(chance) + ',' ) def writeTreasure( self ): self.writeData( "treasure_set.csv", self.reader.treasureset ) def writeTechTrees(self): nodes = [] nodes.extend( self.reader.parts ) nodes.extend( self.reader.missiles ) nodes.extend( self.reader.mines ) nodes.extend( self.reader.probes ) nodes.extend( self.reader.chaff ) nodes.extend( self.reader.factions ) nodes.extend( self.reader.techs ) nodes.extend( self.reader.stations ) nodes.extend( self.reader.ships ) self.allNodes = nodes defMap = {} preMap = {} for i in range(400): defMap[str(i)] = [] preMap[str(i)] = [] uidMap = {} for n in nodes: defs = n.attribs["Def"].split() pres = n.attribs["Pre"].split() tn = TechNode( n ) uidMap[n] = tn for d in defs: defMap[d].append( tn ) for p in pres: preMap[p].append( tn ) self.uidMap = uidMap self.autodefs = [] for i in range(400): if len(defMap[str(i)]) == 0: self.autodefs.append( str(i) ) print "Writing Tech Tree" tfile = file( self.dir + "techtree.txt", "w" ) for n, tn in uidMap.iteritems(): tfile.write( tn.toString() + "\n" ) for pre in tn.getPre().split(): tfile.write( " depends on: " + pre + "\n" ) for dep in defMap[pre]: tn.addParent( dep ) tfile.write( " " + dep.toString() + " OR\n" ) # self.writeFactionTree( self.factions[0] ) def writeFactionTree(self, f): nw = NodeWalker( self.autodefs ) nw.walk( self.uidMap[f] ) class NodeWalker: def __init__(self, defList): self.currentDefs = [] self.currentDefs.append( defList ) self.visitedNodes = [] self.nodeQueue = [] self.indent = 0 def walk(self, node): if( self.meetsDefs( node ) and not self in self.visitedNodes ): print " "*self.indent + node.toString() self.indent += 2 self.visitedNodes.append( self ) defs = self.currentDefs[-1] defs.extend( node.getDef().split() ) self.currentDefs.append( defs ) self.nodeQueue.extend( node.children ) while len(self.nodeQueue) > 0: self.walk(self.nodeQueue.pop(0)) self.indent -= 2 self.currentDefs.pop() def meetsDefs(self, node): for p in node.getPre().split(): if not p in self.currentDefs[-1]: return False return True class TechNode: def __init__(self, entity): self.entity = entity self.parents = [] self.children = [] def getPre(self): return self.entity.attribs["Pre"] def getDef(self): return self.entity.attribs["Def"] def addParent(self, technode): if( technode != self and not technode in self.parents ): self.parents.append( technode ) technode.children.append( self ) def toString(self): return self.entity.toString() class HTMLEntityWriter: def write( self, f, entity, items = None, ignore = [] ): if items == None: items = entity.attribs.keys() f.write( "<TR>" ) for i in items: if entity.attribs.has_key( i ): if type( entity.attribs[ i ] ) is float: f.write( '<TD>' + str( round( entity.attribs[ i ], 2 ) ) + '</TD>' ) else: f.write( '<TD>' + str( entity.attribs[ i ] ) + '</TD>' ) else: f.write( '<TH></TH>' ) if ignore == None: ignore = entity.attribs.keys() for name, value in entity.attribs.iteritems(): if (not name in items) and (not name in ignore): if type( value ) is not float: f.write( '<TD>' + str( value ) + '</TD>' ) else: f.write( '<TD>' + str( round( value, 2 ) ) + '</TD>' ) f.write( "<TR>" ) def sortByAttrib( ents, key ): entMap = {} for e in ents: if not entMap.has_key( e.attribs[key] ): entMap[ e.attribs[key] ] = [ e ] else: entMap[ e.attribs[key] ].append( e ) sList = [] sKeys = entMap.keys() sKeys.sort() for k in sKeys: for s in entMap[ k ]: sList.append( s ) return sList class HTMLWriter: def __init__(self, reader): self.reader = reader def fillData(self, data, ent): d = data[:] for s in re.findall("\[.+?\]",d): d = string.replace(d, s, ent.getAttrib( s[1:-1] )) return d def writeHTMLTOC(self, ents, f): f.write( '<P>\n' ) for e in ents: f.write('- <A HREF="#' + e.getAttrib("Name") + e.getAttrib("UID") + '">' + e.getAttrib("Name") + '</A> ' ) def writeHTMLBody(self, ents, f, template): data = file( 'templates/' + template, "r").read() for e in ents: f.write( self.fillData( data, e ) ) def writeData(self, f, header, template, data): print "Writing", header, len(data), "entities" f.write( "<H1>" + header + "</H1>\n" ) sdata = sortByAttrib( data, "Name" ) self.writeHTMLTOC(sdata, f) self.writeHTMLBody(sdata, f, template) def writeShipsByFaction(self): for f in self.reader.factions: ships = self.reader.filterByFaction( self.reader.ships, f ) ships = sortByAttrib( ships, "Name" ) print "Writing ships for faction", f.toString(), len(ships), "entities" output = file( self.dir + f.attribs["Name"] + ".html", "w" ) output.write( "<H1>" + f.attribs["Name"] + " Ships</H1>\n" ) self.writeHTMLTOC(ships, output) self.writeHTMLBody(ships, output, "ship_template.html" ) def writeEquipment(self): self.writeData( file( self.dir + "probes.html","w"), "Probes", "probe_template.html", self.reader.probes ) self.writeData( file( self.dir + "missiles.html","w"), "Missiles", "missile_template.html", self.reader.missiles ) self.writeData( file( self.dir + "shields.html","w"), "Shields", "shield_template.html", self.reader.shields ) self.writeData( file( self.dir + "boosters.html","w"), "Boosters", "boost_template.html", self.reader.boosters ) self.writeData( file( self.dir + "weapons.html","w"), "Weapons", "weapon_template.html", self.reader.weapons ) self.writeData( file( self.dir + "cloaks.html","w"), "Cloak", "cloak_template.html", self.reader.cloaks ) def write(self): self.dir = self.reader.getCoreName() + '/' try: os.makedirs( self.dir ) except OSError, err: if err.errno != errno.EEXIST: print "Fatal Error:", err return self.writeShipsByFaction() self.writeEquipment() for fname in os.listdir("."): if fname[-4:] == ".igc": print "**** DUMPING", fname, "****" reader = IGCReader() reader.open( fname ) CSVWriter(reader).write() ##if len( sys.argv ) < 2: ## reader.open( "dn_000450.igc" ) ##else: ## reader.open( sys.argv[1] ) ## ##if len( sys.argv ) < 3: ## HTMLWriter(reader).write() ##else: ## if sys.argv[2] == "CSV": ## CSVWriter(reader).write() [/code] | October 17, 2006, 3:42 PM |
Topaz | Why would you do that, there's a CSV module already | October 17, 2006, 8:39 PM |
UserLoser | Visual Basic 6.0? | October 18, 2006, 2:01 AM |
Tontow | [quote author=topaz link=topic=15887.msg159939#msg159939 date=1161117563] Why would you do that, there's a CSV module already [/quote] At the moment I don't know python and that code doesn’t output everything in the file. I also would like to do everything in visual basic 6.0. But if you mean that there is a CSV module already made in visual basic 6.0, then I would like to know where to look for it. [quote author=UserLoser link=topic=15887.msg159955#msg159955 date=1161136908] Visual Basic 6.0? [/quote] The code I posted reads the file and outputs .cvs files but is written in python. I need to be able to read the file and access the data contained in the file using visual basic 6.0 | October 18, 2006, 2:50 AM |
Grok | [quote author=Tontow link=topic=15887.msg159956#msg159956 date=1161139810] But if you mean that there is a CSV module already made in visual basic 6.0, then I would like to know where to look for it. [/quote] I think the Recordset object of ADODB will read a CSV file, and possibly SaveAs one as well. [code] Option Explicit Private cn As New ADODB.Connection Private rs As New ADODB.Recordset Private Sub Form_Load() Dim SQL As String cn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=c:\temp\;Extensions=asc,csv,tab,txt" SQL = "SELECT * FROM file.csv" rs.Open SQL, cn, adOpenStatic, adLockReadOnly, adCmdText Debug.Print "Loaded " & rs.RecordCount & " records." Debug.Print "Column names:" Dim rPos As Long, fPos As Long Dim s As String For fPos = 0 To rs.Fields.Count - 1 Debug.Print rs.Fields(fPos).Name Next Debug.Print "Displaying first 3 records..." rs.MoveFirst For rPos = 0 To 2 s = "" For fPos = 0 To rs.Fields.Count - 1 If Len(s) Then s = s & vbCrLf s = s & rs.Fields(fPos).Value & ", " Next Debug.Print rPos & ": " & s rs.MoveNext Next End Sub [/code] The data looks pretty unintelligible ... are you sure it's a CSV file? | November 8, 2006, 7:12 PM |
Tontow | it reads the file, then outputs csv files with the data. I tryed asking for help on vbforums.com , but I didnt get too far and ended up useing the csv output of CoreDump. http://www.vbforums.com/showthread.php?t=433735 | November 22, 2006, 6:35 AM |