fileinput module not yielding expected results
Jason Friedman
jsf80238 at gmail.com
Sat Sep 7 11:12:19 EDT 2019
import csv
import fileinput
import sys
print("Version: " + str(sys.version_info))
print("Files: " + str(sys.argv[1:]))
with fileinput.input(sys.argv[1:]) as f:
for line in f:
print(f"File number: {fileinput.fileno()}")
print(f"Is first line: {fileinput.isfirstline()}")
I run this:
$ python3 program.py ~/Section*.csv > ~/result
I get this:
$ grep "^Version" ~/result
Version: sys.version_info(major=3, minor=7, micro=1, releaselevel='final',
serial=0)
$ grep "^Files" ~/result
Files: ['/home/jason/Section01.csv', '/home/jason/Section02.csv',
'/home/jason/Section03.csv', '/home/jason/Section04.csv',
'/home/jason/Section05.csv', '/home/jason/Section06.csv']
$ grep -c "True" ~/result
6
That all makes sense to me, but this does not:
$ grep "File number" ~/result | sort | uniq
File number: 3
I expected that last grep to yield:
File number: 1
File number: 2
File number: 3
File number: 4
File number: 5
File number: 6
My ultimate goal is as follows. I have multiple CSV files, each with the
same header line. I want to read the header line from the first file and
ignore it for subsequent files.
Thank you
More information about the Python-list
mailing list