in reply to Few lines in CSV file are skipped

But many lines in of the file "finalidlist" that consists of PubMed IDs is skipped. Can anyone help me whats the problem?

Maybe the problem is not in the code but is in the finalidlist file which you haven't posted. If your example isn't self-contained then it cannot be tested. See SSCCE.

In the meantime:

See the Basic debugging checklist for why these things matter.