[Tutor] Invisible Characters in Fortran/Python
Francesco Loffredo
fal at libero.it
Mon Apr 11 10:09:40 CEST 2011
On 04/10/2011 0.12, Tyler Glembo wrote:
> Hi All,
> So I have a ~3000 line fortran code that needs to be updated to run new files by simply updating a few lines in the code (~10
> lines). I thought python would be a great way to do so since I know a little python but not fortran. So, my plan was to read in
> each line, and then at a certain line number, write out the changed code. A short snippet is as follows:
>
> dest= open( f1, "w" )
> source= open( f2, "r" )
> for line in source:
> if X:
> dest.write( newline + "\n" )
> else:
> dest.write( line )
> dest.close()
> source.close()
>
> The problem I am having is with hidden/invisible character. In the fortran code, there are line indents which are denoted with an
> invisible character ^I. When I write with python, there is no ^I at the beginning of the line and the fortran code no longer
> compiles. I know how to put in the invisible line return character (\n), but how can I put in other invisible characters?
I think you could avoid entirely this problem by using the binary option in the open function:
dest= open( f1, "wb" )
source= open( f2, "rb" )
This way, each read and each write you perform will take into account all kinds of characters, and so your written lines will be
identical to those you read. If the problem was only with the "newline" lines, then Steven's answer has already solved your problem.
Hope that helps
Francesco
-----
Nessun virus nel messaggio.
Controllato da AVG - www.avg.com
Versione: 10.0.1209 / Database dei virus: 1500/3564 - Data di rilascio: 10/04/2011
More information about the Tutor
mailing list