2
I have a tab-separated file in which the first lines are comments designated with '#'. I use the following code to pull the file without the comments... The file is something like:
#comentario
#comentario
#comentario
#comentario
#comentario
Header1 Header2 Header3
a b c
d e f
g h i
And then I use the code below to load it without the comments...
import pandas as pd
file_in = pd.read_table('arquivo.tsv', comment='#')
In this way:
Header1 Header2 Header3
a b c
d e f
g h i
After that I make some changes to the Header1 column based on information from another file, and rewrite the file file_in
:
file_in.to_csv('arquivo.csv', index=False, sep='\t')
The point here is that I would like the comments to return as in the original, but the saved file starts with Header and no longer with the comments!
I understand Luiz Vieira! In this case everything I want to make changes to my file
file_in
i realize betweenfile_in = pd.read_table('arquivo.tsv', comment=commentChar)

andwith open('arquivo.csv', 'w') as f:
 for comment in comments:
 f.write(comment)
– guidebortoli
No, no. The
with open('arquivo.csv', 'w')
open the file for recording, then only afterward of that line is that you can record anything (and, of course, while within thewith
).– Luiz Vieira