从 for 循环写入多个 CSV 文件
Writing to multiple CSV files from a for-loop
我有一个获取股票代码的 for loop
和另一个获取股票数据的 for 循环。现在我正在尝试创建 csv 文件,该文件将从第一个 for 循环中获取股票名称数据,并将股票数据添加到 CSV 文件中。
stock_data = []
with open('Nifty 50 Scrapped data.csv') as csvfile:
stockticker_data = csv.reader(csvfile, delimiter=' ')
for row in stockticker_data:
# print(row)
all_data = []
for ticker in row:
stock_data.append(web.get_data_yahoo(ticker, '1/1/2018', '1/1/2019'))
with open(ticker, 'w') as f:
f = open(ticker,'w')
f.write(stock_data)
f.close()
我收到以下错误:
f.write(stock_data)
TypeError: write() argument must be str, not list
print(stock_data)
的输出:
[ High Low ... Volume Adj Close
Date ...
2018-01-01 1165.000000 1138.099976 ... 591349.0 1127.366211
2018-01-02 1150.000000 1134.050049 ... 516171.0 1126.479004
2018-01-03 1149.000000 1135.300049 ... 593809.0 1125.641235
2018-01-04 1178.000000 1145.900024 ... 729965.0 1155.361816
2018-01-05 1192.000000 1167.449951 ... 1151320.0 1168.373901
... ... ... ... ... ...
2018-12-27 1384.750000 1354.300049 ... 2174090.0 1362.613403
2018-12-28 1383.000000 1359.000000 ... 1705033.0 1356.160278
2018-12-31 1378.000000 1367.300049 ... 698593.0 1363.159546
2019-01-01 1379.699951 1358.599976 ... 664707.0 1361.670410
2019-01-02 1386.849976 1361.599976 ... 1233780.0 1373.335693
[248 rows x 6 columns]]
[ High Low ... Volume Adj Close
Date ...
2018-01-01 1165.000000 1138.099976 ... 591349.0 1127.366211
2018-01-02 1150.000000 1134.050049 ... 516171.0 1126.479004
2018-01-03 1149.000000 1135.300049 ... 593809.0 1125.641235
2018-01-04 1178.000000 1145.900024 ... 729965.0 1155.361816
2018-01-05 1192.000000 1167.449951 ... 1151320.0 1168.373901
... ... ... ... ... ...
2018-12-27 1384.750000 1354.300049 ... 2174090.0 1362.613403
2018-12-28 1383.000000 1359.000000 ... 1705033.0 1356.160278
2018-12-31 1378.000000 1367.300049 ... 698593.0 1363.159546
2019-01-01 1379.699951 1358.599976 ... 664707.0 1361.670410
2019-01-02 1386.849976 1361.599976 ... 1233780.0 1373.335693
此错误告诉您 stock_data
是一个 list
,而 write()
方法需要一个 str
。知道 web.get_data_yahoo()
返回的数据是 pd.DataFrame
如何解决?我们可以像这样使用 pd.to_csv
来做到这一点:
stock_data = []
with open('Nifty 50 Scrapped data.csv') as csvfile:
stockticker_data = csv.reader(csvfile, delimiter=' ')
for row in stockticker_data:
# print(row)
all_data = []
for ticker in row:
stock_data.append(web.get_data_yahoo(ticker, '1/1/2018', '1/1/2019'))
for df in stock_data:
df.to_csv(ticker, header=None, index=None, sep=' ', mode='a')
我有一个获取股票代码的 for loop
和另一个获取股票数据的 for 循环。现在我正在尝试创建 csv 文件,该文件将从第一个 for 循环中获取股票名称数据,并将股票数据添加到 CSV 文件中。
stock_data = []
with open('Nifty 50 Scrapped data.csv') as csvfile:
stockticker_data = csv.reader(csvfile, delimiter=' ')
for row in stockticker_data:
# print(row)
all_data = []
for ticker in row:
stock_data.append(web.get_data_yahoo(ticker, '1/1/2018', '1/1/2019'))
with open(ticker, 'w') as f:
f = open(ticker,'w')
f.write(stock_data)
f.close()
我收到以下错误:
f.write(stock_data)
TypeError: write() argument must be str, not list
print(stock_data)
的输出:
[ High Low ... Volume Adj Close
Date ...
2018-01-01 1165.000000 1138.099976 ... 591349.0 1127.366211
2018-01-02 1150.000000 1134.050049 ... 516171.0 1126.479004
2018-01-03 1149.000000 1135.300049 ... 593809.0 1125.641235
2018-01-04 1178.000000 1145.900024 ... 729965.0 1155.361816
2018-01-05 1192.000000 1167.449951 ... 1151320.0 1168.373901
... ... ... ... ... ...
2018-12-27 1384.750000 1354.300049 ... 2174090.0 1362.613403
2018-12-28 1383.000000 1359.000000 ... 1705033.0 1356.160278
2018-12-31 1378.000000 1367.300049 ... 698593.0 1363.159546
2019-01-01 1379.699951 1358.599976 ... 664707.0 1361.670410
2019-01-02 1386.849976 1361.599976 ... 1233780.0 1373.335693
[248 rows x 6 columns]]
[ High Low ... Volume Adj Close
Date ...
2018-01-01 1165.000000 1138.099976 ... 591349.0 1127.366211
2018-01-02 1150.000000 1134.050049 ... 516171.0 1126.479004
2018-01-03 1149.000000 1135.300049 ... 593809.0 1125.641235
2018-01-04 1178.000000 1145.900024 ... 729965.0 1155.361816
2018-01-05 1192.000000 1167.449951 ... 1151320.0 1168.373901
... ... ... ... ... ...
2018-12-27 1384.750000 1354.300049 ... 2174090.0 1362.613403
2018-12-28 1383.000000 1359.000000 ... 1705033.0 1356.160278
2018-12-31 1378.000000 1367.300049 ... 698593.0 1363.159546
2019-01-01 1379.699951 1358.599976 ... 664707.0 1361.670410
2019-01-02 1386.849976 1361.599976 ... 1233780.0 1373.335693
此错误告诉您 stock_data
是一个 list
,而 write()
方法需要一个 str
。知道 web.get_data_yahoo()
返回的数据是 pd.DataFrame
如何解决?我们可以像这样使用 pd.to_csv
来做到这一点:
stock_data = []
with open('Nifty 50 Scrapped data.csv') as csvfile:
stockticker_data = csv.reader(csvfile, delimiter=' ')
for row in stockticker_data:
# print(row)
all_data = []
for ticker in row:
stock_data.append(web.get_data_yahoo(ticker, '1/1/2018', '1/1/2019'))
for df in stock_data:
df.to_csv(ticker, header=None, index=None, sep=' ', mode='a')