获取名为变量的文件行

时间:2017-01-06 01:00:12

标签: python python-3.x

我有一个包含代码的文件,用新行分隔。因此,当打印文件时,它看起来像这样:

 SPY

 QQQ

我正在尝试使用雅虎财务模块来获取每个股票代码的价格。但是,我无法正确构造for循环,因此只包含自动收报机(括号和\n)。我只需要声明代码(var2 = Share('SPY') , var2 = Share('QQQ'))。任何帮助将非常感激。我目前的代码在

之下
from yahoo_finance import Share

file = open('positions.txt', 'r')
result = [line.split(',') for line in file.readlines()]

for ticker in result:
    var = ticker
    var1 = str(ticker)
    #the yahoo finance functions are below
    var2 = Share(var1)
    print(var2.get_price())

1 个答案:

答案 0 :(得分:0)

问题可能来自您解析 private void button2_Click(object sender, EventArgs e) { //Hash var hash = SecurePasswordHasher.Hash("password"); //Verify var result = SecurePasswordHasher.Verify("password", hash); if ( txtUsername.Text == "" || txt_Password.Text == "") { MessageBox.Show("Please provide a Username and Password"); return; } try { //Create SqlConnection SqlConnection con = new SqlConnection(cs); SqlCommand cmd = new SqlCommand("Select * from [break].[dbo].[tabl_login] where UserName=@username and Password=@password", con); cmd.Parameters.AddWithValue("@username", txtUsername.Text); cmd.Parameters.AddWithValue("@password", txt_Password.Text); con.Open(); SqlDataAdapter adapt = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapt.Fill(ds); con.Close(); int count = ds.Tables[0].Rows.Count; //If count is equal to 1, than show frmMain form if (count == 1) { MessageBox.Show("Login Successful!"); if (!new WebClient().DownloadString( "ftp://username:password@wdasd.bplaced.net/test.txt").Contains("1.0.0.0")) { } else { if (MessageBox.Show("New Update! Would you like to update?", "Yay!", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes) { Process.Start("ftp://username:password@wdasd.bplaced.net/wdasd.bplaced.net.zip"); } } } else { MessageBox.Show("Login Failed!"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } 文件的方式。如果它实际上是由新行分隔的代码的文本文件,那么readlines()应该给你一个类似下面的列表和(在一些代码清理之后),它适用于我。

positions.txt

以下是我的建议,但我需要查看您的文件示例,以确定:

>>> result = ['SPY', 'QQQ']
>>> for ticker in result:
...     var2 = Share(ticker)
...     print(var2.get_price())
...
226.58
120.87