0

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
Michael T Johnson
  • 679
  • 2
  • 13
  • 26
  • *Always* use the Python generic tag. – juanpa.arrivillaga Dec 08 '17 at 22:22
  • So you want `data` to be a string that is essentially your `csv` with delimiters replaced by single-spaces? – juanpa.arrivillaga Dec 08 '17 at 22:23
  • 1
    To help me know how specific and detailed to make the answer, the first script that you have there, do you understand how it works or did you just copy it from somewhere? If you understand the mechanics of that script, the answer will be a lot easier to explain, however if you don't, a python tutorial will be more your speed. – Davy M Dec 08 '17 at 22:23
  • I used docs.python.org for the structure. Yes juanpa, i believe that is what i would like. Instead of "value" It should be a string. I will edit the name. – Michael T Johnson Dec 08 '17 at 22:25
  • The code would rise indentation error, to start with. –  Dec 08 '17 at 22:29
  • How are the data in your input file delimited? Is the delimiter a comma or some other character? Is character data single, double, or quoted at all? – drsnark Dec 08 '17 at 22:30
  • I understand some of it Davy M, just need to convert it to a string i believe – Michael T Johnson Dec 08 '17 at 22:53

1 Answers1

3

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
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Thanks! I just figured it out at as well. Perhaps you could help explain the difference? Mine is `for row in reader:` `data = (' '.join(row))` `print(data)` – Michael T Johnson Dec 08 '17 at 23:12
  • Michael: The part that joins the items is identical. but the code in my answer joins the results from each row together in a long string and with newline characters between them. I would think it would be obvious from the output... – martineau Dec 08 '17 at 23:17
  • It seems like yours finishes more quickly in IDLE.. Am i just imagining that? The only difference being the `'\n'` which means newline character? – Michael T Johnson Dec 08 '17 at 23:17
  • Yes, `'\n'` is a newline character. You can time it to compare speed of execution. In my version `print()` is only called once, not for every row in the csv file. – martineau Dec 08 '17 at 23:18
  • Is there a way to ignore the header Martineau? – Michael T Johnson Dec 09 '17 at 02:02
  • Michael: Yes there is...see updated question. If you want to do it "intelligently"—as in only when one is present—see [my answer](https://stackoverflow.com/a/11350095/355230) to a related question which uses the [`csv.Sniffer`](https://docs.python.org/3/library/csv.html#csv.Sniffer) class which makes it relatively easy to do so. – martineau Dec 09 '17 at 05:11