numpy - how to import .txt contents into a line of code in python 2.7 -
i'm python newb, tried searching if there other similar questions doesn't seem there are. i'm hoping question isn't ridiculous, , why didn't find others.
i have .txt file 500 stock symbols have include in code create event matrix. i'm wondering if there way can somehow import symbols line of code without having type each individual symbol out. i'm using python out of terminal if makes difference.
also running on virtual machine on ubuntu 12.04
the line of code looks this.
symbols = [""]
also unrelated original question illustration code shows this.
print ''name'' + "reading data"
do underlines represent else , shows in .py, seems run error when trying out.
thank you.
this assumes file of form
aapl msft ibm
and on. , file named tickers.txt
.
to in pandas
do
import pandas pd tickers = pd.read_csv('tickers.txt', names=['tickers'])
this load file dataframe.
to in numpy
do
import numpy np tickers = np.loadtxt('ticker.txt', dtype=str)
this load file numpy array.
the above solutions work, it's overkill simple task you're doing. i'd utilize vanilla python task.
with open('tickers.txt') f: tickers = f.readlines()
numpy python-2.7 matplotlib pandas
No comments:
Post a Comment