Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 用Python解析数据输出_Python 3.x_Yahoo Finance - Fatal编程技术网

Python 3.x 用Python解析数据输出

Python 3.x 用Python解析数据输出,python-3.x,yahoo-finance,Python 3.x,Yahoo Finance,所以我有这个代码: si.get_stats("aapl") 返回此垃圾: 0 Market Cap (intraday) 5 877.04B 1 Enterprise Value 3 966.56B 2 Trailing P/E 15.52 3

所以我有这个代码:

si.get_stats("aapl")
返回此垃圾:

0                          Market Cap (intraday) 5       877.04B
1                               Enterprise Value 3       966.56B
2                                     Trailing P/E         15.52
3                                    Forward P/E 1         12.46
4                      PEG Ratio (5 yr expected) 1          1.03
5                                Price/Sales (ttm)          3.30
6                                 Price/Book (mrq)          8.20
7                       Enterprise Value/Revenue 3          3.64
8                        Enterprise Value/EBITDA 6         11.82
9                                 Fiscal Year Ends  Sep 29, 2018
10                       Most Recent Quarter (mrq)  Sep 29, 2018
11                                   Profit Margin        22.41%
12                          Operating Margin (ttm)        26.69%
13                          Return on Assets (ttm)        11.96%
14                          Return on Equity (ttm)        49.36%
15                                   Revenue (ttm)       265.59B
16                         Revenue Per Share (ttm)         53.60
17                  Quarterly Revenue Growth (yoy)        19.60%
18                              Gross Profit (ttm)       101.84B
19                                          EBITDA         81.8B
20                  Net Income Avi to Common (ttm)        59.53B
21                               Diluted EPS (ttm)         11.91
22                 Quarterly Earnings Growth (yoy)        31.80%
23                                Total Cash (mrq)         66.3B
24                      Total Cash Per Share (mrq)         13.97
25                                Total Debt (mrq)       114.48B
26                         Total Debt/Equity (mrq)        106.85
27                             Current Ratio (mrq)          1.12
28                      Book Value Per Share (mrq)         22.53
29                       Operating Cash Flow (ttm)        77.43B
30                    Levered Free Cash Flow (ttm)        48.42B
31                               Beta (3Y Monthly)          1.21
32                                52-Week Change 3         5.27%
33                         S&P500 52-Week Change 3         4.97%
34                                  52 Week High 3        233.47
35                                   52 Week Low 3        150.24
36                         50-Day Moving Average 3        201.02
37                        200-Day Moving Average 3        203.28
38                             Avg Vol (3 month) 3         38.6M
39                              Avg Vol (10 day) 3        42.36M
40                            Shares Outstanding 5         4.75B
41                                           Float         4.62B
42                            % Held by Insiders 1         0.07%
43                        % Held by Institutions 1        61.16%
44                   Shares Short (Oct 31, 2018) 4        36.47M
45                    Short Ratio (Oct 31, 2018) 4          1.06
46               Short % of Float (Oct 31, 2018) 4         0.72%
47  Short % of Shares Outstanding (Oct 31, 2018) 4         0.77%
48       Shares Short (prior month Sep 28, 2018) 4         40.2M
49                  Forward Annual Dividend Rate 4          2.92
50                 Forward Annual Dividend Yield 4         1.51%
51                 Trailing Annual Dividend Rate 3          2.72
52                Trailing Annual Dividend Yield 3         1.52%
53                 5 Year Average Dividend Yield 4          1.73
54                                  Payout Ratio 4        22.84%
55                                 Dividend Date 3  Nov 15, 2018
56                              Ex-Dividend Date 4   Nov 8, 2018
57               Last Split Factor (new per old) 2           1/7
58                               Last Split Date 3   Jun 9, 2014
这是一个第三方功能,从Yahoo Finance中删除数据。我需要这样的东西

def func( si.get_stats("aapl") ):
     **magic**
     return Beta (3Y Monthly)

具体来说,我希望它返回与Beta关联的数字,而不是实际文本。

我假设函数调用为表中的每一行返回一个字符串或字符串列表,而不是写入标准输出

要获取与Beta(每月3次)或任何其他参数名称相关的值,请执行以下操作:

1) 如果返回值是一个字符串,其格式如上表所示,则应在每行末尾\n显示。因此,您可以将该字符串拆分为一个列表,然后迭代查找参数名,然后再次拆分以获取与其关联的数字

# Split the single formatted string to a list of elements, each element
# is one line in the table
str_lst = si.get_stats("aapl").split('\n')
for line in str_lst:
    # change Beta (3Y Monthly) to any other parameter required.
    if 'Beta (3Y Monthly)' in line:
        # split this line with the default split value of white space
        # this should provide a list of elements split at each white space.
        # eg : ['31', 'Beta', '(3Y', 'Monthly)', '1.21'], the numeric value is the
        # last element. Strip to remove trailing space/newline.
        num_value_asStr = line.split()[-1].strip()
return num_value_asStr 
2) 如果它已经返回了一个列表,那么只需迭代列表项并使用上面的If条件,拆分所需的列表元素以获得与参数关联的数值

str_lst = si.get_stats("aapl")
for line in str_lst:
    # change Beta (3Y Monthly) to any other parameter required.
    if 'Beta (3Y Monthly)' in line:
        # split this line with the default split value of white space
        # this should provide a list of elements split at each white space.
        # eg : ['31', 'Beta', '(3Y', 'Monthly)', '1.21'], the numeric value is the
        # last element. Strip to remove trailing space/newline.
        num_value_asStr = line.split()[-1].strip()
return num_value_asStr 

你试过什么没用的?