Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.
Originally posted by: jpstoryHi
I have the following code written to:
1. Skip the pre-header lines and grab the real header line (where a string of "XYZ" is found)
2. ReWrite the entire text file with the real header line as the first line so I can use CSV node to import the files in later step
It just does not work and I couldn't figure out what went wrong, please kindly advise, thanks.
import braininfo
import csv
def setup(brainNodeControlObj, BrainNodeClass):
class BrainNode(BrainNodeClass):
def initialize(self):
super(BrainNode, self).initialize()
metadata = self.newMetadata()
metadata.append("FileNames", "String")
self.outputs[0].metadata = metadata
def finalize(self, val):
super(BrainNode, self).finalize(val)
def pump(self, quant):
while quant.permitsRunning(self):
inRec = self.inputs[0].read()
if not inRec:
return False
filename = inRec["FileName"]
r = 0
with open(filename,'rU') as f:
reader = csv.reader(f)
for row in reader:
if row.find("XYZ") <> -1:
hd = row.split(",")
break
success = self.Process(filename,f,hd,reader)
def Process(self, filename,f,hd,reader):
success = False
outRec = self.outputs[0].newRecord()
#OverWrite the source file
with open(filename,'w') as f:
writer = csv.DictWriter(f,fieldnames=hd)
writer.writeheader()
for data in reader
writer.writerow(data)
outRec["FileNames"]=filename
self.outputs[0].write(outRec)
success = True
return BrainNode