0

My programming professor and the internet say I must always append exactly one empty line (=trailing newline) to all my output text files.

Why does

my_df = pd.read_csv('my.tsv', delimiter='\t', skip_blank_lines=True)

still read this \n into my pandas data frames and fills it with NaN values?

So far I have used df_no_nan = df.dropna(how="any", inplace=False).

enter image description here

What am I missing?

Is there a better solution?

Here the WeTransfer link to the file:

https://wetransfer.com/downloads/d34deac70e33a0a6351b8e4d6b4e3b8720210602143836/74fa00

ilam engl
  • 1,310
  • 1
  • 9
  • 22
  • 1
    I don't think I've ever seen this behavior. Can you provide a [mcve]? – juanpa.arrivillaga Jun 01 '21 at 16:27
  • @juanpa.arrivillaga Thank you, I've edited the post and added the exact file I'm using. The link will expire in 7 days. – ilam engl Jun 02 '21 at 14:43
  • 2
    I think you may be confusing yourself a little with the interpretation of the rule - it's that files are supposed to end with a `\n` that terminates the last line of the file, not that they need to end with a completely blank line (i.e., `\n\n`). – Randy Jun 02 '21 at 15:05
  • @Randy indeed, I got the file from my supervisor - it had 4 empty lines at the end. I cut 3 of those out - to observe the 'trailing empty line' rule. So you're saying I could have just cut all 4 empty lines that appeared? That is confusing to me as I'm quite sure I was taught to always hit `enter` in vim to add an extra line to each file - from what you say, I take that is wrong too? – ilam engl Jun 03 '21 at 08:59
  • 1
    Yeah, that is wrong. vim follows the POSIX standard and will put the `\n` at the end of your last line for you when you save the file. You can check this yourself if you're curious by making a simple one-line file in vim, saving it, and running it through `hexdump`. The last byte will be `0a`, or `\n` in ASCII. – Randy Jun 03 '21 at 14:55

0 Answers0