Reputation: 689
I have a script:
import csv
with open('2017020397.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
print(' '.join(row))
Output looks like this:
LastName StartTime EndTime Duration Period TeamAbbrev Pos
Bouwmeester 0:00 0:37 0:37 1 STL D
Schwartz 0:00 0:40 0:40 1 STL W
Foligno 0:00 0:40 0:40 1 MIN W
Pietrangelo 0:00 0:48 0:48 1 STL D
Suter 0:00 0:40 0:40 1 MIN D
Instead of printing the rows, I would like the rows to equal data. Then I can just say print(data)
. New to python and writing scripts.
Upvotes: 0
Views: 64
Reputation: 123491
This does it:
import csv
with open('2017020397.csv', newline='') as f:
data = '\n'.join(' '.join(row) for row in csv.reader(f))
print(data)
Output:
LastName StartTime EndTime Duration Period TeamAbbrev Pos
Bouwmeester 0:00 0:37 0:37 1 STL D
Schwartz 0:00 0:40 0:40 1 STL W
Foligno 0:00 0:40 0:40 1 MIN W
Pietrangelo 0:00 0:48 0:48 1 STL D
Suter 0:00 0:40 0:40 1 MIN D
In order to leave the header out, you'd need to do it slightly differently:
with open('2017020397.csv', newline='') as f:
reader = csv.reader(f)
next(reader) # Skip header row.
data = '\n'.join(' '.join(row) for row in reader)
print(data)
Output ignoring header:
Bouwmeester 0:00 0:37 0:37 1 STL D
Schwartz 0:00 0:40 0:40 1 STL W
Foligno 0:00 0:40 0:40 1 MIN W
Pietrangelo 0:00 0:48 0:48 1 STL D
Suter 0:00 0:40 0:40 1 MIN D
Upvotes: 3