1

The Code:

import csv

symbols = []
with open("I:/my500.csv") as f:
    for row in f:
        symbols.append(row)

The output:

['SPY\n', 'WMT\n', 'GLD\n', 'IBM\n']
iz_
  • 15,923
  • 3
  • 25
  • 40
Pokey
  • 11
  • 2

1 Answers1

0

That's because when you iterate through a file, the lines contain a newline. To solve this, you can .rstrip() them.

symbols = []
with open("I:/my500.csv") as f:
    for row in f:
        symbols.append(row.rstrip('\n'))

Even better, you can use a list comprehension:

with open("I:/my500.csv") as f:
    symbols = [row.rstrip('\n') for row in f]
iz_
  • 15,923
  • 3
  • 25
  • 40