在Windows 10通過遍歷文件讀取
我有一個叫upgrade.csv文件,其中包含此使用Python 3.5.2 ::蟒蛇4.0.0(64位):
['$APPL','$FB','$MSLI','$AMZN','$NVDA','$WMT','$NFLX']
我想遍歷每個這些股票代碼。
從與下面的代碼返回該文件拉:
[
'
$
A
P
P
L
'
,
'
$
F
B
'
,
等通過數據。
第一代碼:
file = open('upgrades.csv', 'r')
upgrades = (file.read())
file.close()
upgrades = list(upgrades)
for i in range(len(upgrades)):
print(upgrades[i])
從顯式串拉回到我想要什麼:
$APPL
$FB
$MSLI
$AMZN
$NVDA
$WMT
$NFLX
第二碼:
upgrades = ['$APPL','$FB','$MSLI','$AMZN','$NVDA','$WMT','$NFLX']
upgrades = list(upgrades)
for i in range(len(upgrades)):
print(upgrades[i])
請幫我從文件中提取數據,並得到這個結果:
$APPL
$FB
$MSLI
$AMZN
$NVDA
$WMT
$NFLX
我已經試過轉換數據,如:
upgrades = list(upgrades)
upgrades = str(upgrades)
upgrades = tuple(upgrades)
我也試着從CSV改變文件爲txt。
嘗試使用Google ReadLine方法。這種方法可以用於按行讀取文件行 –
謝謝Arpit –