canopy / ipython运行脚本 - 没有输出?

时间:2016-02-04 05:13:19

标签: python ipython canopy

我是IPython的新手,但不是py本身的新手。我将从一本名为datadrivensecurity的书中学习一些代码示例,并尝试运行其中一个代码示例。当我在IPython中创建一个新文件(使用cannopy),然后单击运行,我在控制台窗口中获得以下输出。

In [9]: %run /Users/myuser/Documents/Notebooks/ch02.py
highvulns     int64
name         object
os           object
dtype: object
In [10]:

当我将代码复制/粘贴到In[#]控制台提示符时,我得到了预期的输出。我做错了什么?

#
# name ch02.py
#

# create a new data frame

import numpy as np
import pandas as pd

# create a new data frame of hosts & high vuln counts
assets_df = pd.DataFrame( { 
    "name" : ["danube","gander","ganges","mekong","orinoco" ],
    "os" : [ "W2K8","RHEL5","W2K8","RHEL5","RHEL5" ],
    "highvulns" : [ 1,0,2,0,0 ] 
    } )

# take a look at the data frame structure & contents
print(assets_df.dtypes)
assets_df.head()

# show a "slice" just the operating systmes
assets_df.os.head()

# add a new column
assets_df['ip'] = [ "192.168.1.5","10.2.7.5","192.168.1.7",
                     "10.2.7.6", "10.2.7.7" ]

# show only nodes with more than one high vulnerabilty               
assets_df[assets_df.highvulns>1].head()

# divide nodes into network 'zones' based on IP address
assets_df['zones'] = np.where(
    assets_df.ip.str.startswith("192"), "Zone1", "Zone2")

# get one final view
assets_df.head()

highvulns     int64
name         object
os           object
dtype: object



Out[7]: 


highvulns
name
os
ip
zones
0
1
danube
W2K8
192.168.1.5
Zone1
1
0
gander
RHEL5
10.2.7.5
Zone2
2
2
ganges
W2K8
192.168.1.7
Zone1
3
0
mekong
RHEL5
10.2.7.6
Zone2
4
0
orinoco
RHEL5
10.2.7.7
Zone2

1 个答案:

答案 0 :(得分:1)

为方便起见,如果在提示符下键入表达式,则将打印表达式的值。但是如果你只是在python文件中编写相同的表达式,它将被评估,但不会打印该值。如果您希望从正在运行的文件中打印x的值,则应print x