[Tutor] print to file probelm

Prasad, Ramit ramit.prasad at jpmorgan.com
Mon Aug 15 22:09:34 CEST 2011


Hi John,

Just as a note, I am looking at the included scripts (and not the linked scripts). I believe the problem you are having is with the lines below. 


for i in range(len(faces)):
     f = faces[flist[i]]
     n = normals[flist[i]]
     s = s + ",%d" % len(f)
     for v in f:
         s = s + ",%d" % vindex[v]
     s = s + ",%.17g,%.17g,%.17g" % (n[0], n[1], n[2])
s = s + "]"
print s

The problem is probably caused by the very last line above. This prints 's' to std out (the console) instead of a file. This should probably be something like  (not tested - just a guide)
outfile.write(s)
outfline.close()


I would also like to add that string concatenation is not recommended in Python. The better way to concatenate is to do something like:

S = []
s.append( 'test' )
s.append( ",{0}".format( vindex[v] ) )
s.append( ",%.17g,%.17g,%.17g" % (v[0], v[1], v[2]) )
# Join at the very end
" ".join(s)


Furthermore, the following can be easily written without the backslash (which can be prone to errors if something appends anything after it (like non-CRLR whitespace --or whatever Windows uses). Instead you can wrap things is parenthesis

normals[sl[1]] = \
             (string.atof(sl[2]), string.atof(sl[3]), string.atof(sl[4]))

Try this instead:
normals[sl[1]] = (string.atof(sl[2]), 
             string.atof(sl[3]), string.atof(sl[4]))


OR if you have a single long string. 
Var = ( self.really_long_variable_name + some_other_really_long_variable + 
		what_another_really_long_variable ) # returns a single element

Be careful not to put a comma at the end of the parenthesis, because if you do the statement will return a tuple instead.

Var = ( self.really_long_variable_name + some_other_really_long_variable + 
		what_another_really_long_variable, ) # returns a tuple with one element



Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list