代码:
import csv
symbols = []
with open("I:/my500.csv") as f:
for row in f:
symbols.append(row)
输出:
['SPY\n', 'WMT\n', 'GLD\n', 'IBM\n']
答案 0 :(得分:0)
这是因为当您遍历文件时,这些行包含换行符。为了解决这个问题,您可以.rstrip()
。
symbols = []
with open("I:/my500.csv") as f:
for row in f:
symbols.append(row.rstrip('\n'))
更好的是,您可以使用列表理解:
with open("I:/my500.csv") as f:
symbols = [row.rstrip('\n') for row in f]