在本文中,我们将给您介绍关于在ipython笔记本中读取单元格内容的详细内容,并且为您解答macpython读取txt文件的相关问题,此外,我们还将为您提供关于anaconda–如何在IPythonj
在本文中,我们将给您介绍关于在ipython笔记本中读取单元格内容的详细内容,并且为您解答macpython读取txt文件的相关问题,此外,我们还将为您提供关于anaconda – 如何在IPython jupyter笔记本中传递命令行参数、html – 如何更改ipython笔记本中的字体、IPtthon笔记本在plt.show()之后停止评估单元格、ipython-notebook – iPython笔记本不支持VPN的知识。
本文目录一览:- 在ipython笔记本中读取单元格内容(macpython读取txt文件)
- anaconda – 如何在IPython jupyter笔记本中传递命令行参数
- html – 如何更改ipython笔记本中的字体
- IPtthon笔记本在plt.show()之后停止评估单元格
- ipython-notebook – iPython笔记本不支持VPN
在ipython笔记本中读取单元格内容(macpython读取txt文件)
我有一个ipython
笔记本电脑,里面有混合markdown
和python
单元格。
我希望我的一些python
单元格读取相邻的markdown
单元格并将其作为输入进行处理。
所需情况的一个示例:
CELL 1(降价) :要执行的SQL代码
CELL 2(降价) :
select * from tbl where x=1
细胞3(python) :
mysql.query(ipython.previous_cell.content)
(语法ipython.previous_cell.content
组成)
执行“ CELL 3 ”应等效于mysql.query("select * from tbl where x=1")
如何才能做到这一点 ?
答案1
小编典典我认为您正在尝试以错误的方式解决问题。
首先是的,有可能以一种真正骇人听闻的方式获得相邻的markdown单元,这在无头笔记本执行中是行不通的。
您要执行的操作是使用IPython单元魔术,该魔术允许任意语法,只要该单元以2个百分号开头,后跟一个标识符即可。
通常,您需要SQL单元格。
您可以参考有关细胞魔术的文档,
或者我可以向您展示如何构建它:
from IPython.core.magic import ( Magics, magics_class, cell_magic, line_magic)@magics_classclass StoreSQL(Magics): def __init__(self, shell=None, **kwargs): super().__init__(shell=shell, **kwargs) self._store = [] # inject our store in user availlable namespace under __mystore # name shell.user_ns[''__mystore''] = self._store @cell_magic def sql(self, line, cell): """store the cell in the store""" self._store.append(cell) @line_magic def showsql(self, line): """show all recorded statements""" print(self._store) ## use ipython load_ext mechanisme here if distributed get_ipython().register_magics(StoreSQL)
现在,您可以在python单元格中使用SQL语法:
%%sql select * from foo Where QUX Bar
第二个单元格:
%%sqlInsert Cheezburger into Can_I_HAZ
检查我们执行了什么(3个破折号显示了输入/输出定界,您不必键入它们):
%showsql---[''select * from foo Where QUX Bar'', ''Insert Cheezburger into Can_I_HAZ'']
在问题开始时您问了什么:
mysql.query(__mystore[-1])
当然,这确实需要您以正确的顺序执行先前的单元格,没有什么阻止您使用%%sql
语法来命名您的单元格,例如,如果_store
是dict
或更好的是您覆盖的类,则__getattr__
类似于__getitem__
使用点语法访问字段。这留给读者练习,或者最终看一下答案:
@cell_magicdef sql(self, line, cell): """store the cell in the store""" self._store[line.strip()] = cell
然后您可以使用像
%%sql A1set foo TO Bar where ID=9
然后在您的Python单元中
mysql.execute(__mystore.A1)
我也强烈建议看着凯瑟琳Develin
SqlMagic为IPython的,这笔记本要点在GitHub上,实况表演这一切事情。
在您似乎要说的评论中pig
,没有什么可以阻止您拥有%%pig
魔力的。也可以注入Javascript以启用SQL和PIG的正确语法高亮显示,但这超出了此问题的范围。
anaconda – 如何在IPython jupyter笔记本中传递命令行参数
我希望在argparse模块的帮助下将几个参数传递给我的工作脚本.
下面是代码..
import argparse parser = argparse.ArgumentParser(description = ''Process display arguments'') parser.add_argument(''-t'',"--test_name",help="Mandatory test name directory path",type=str) parser.add_argument(''-s'',"--symbolSet",nargs = ''?'',help="Optional symbolset",const = ''baz'',default = ''deafultOne'') args = parser.parse_args() if args.test_name is None: print("test_name argument is mandatory.. Use option -h for help.. Exiting..") sys.exit(1)
现在,如何通过UI执行我的脚本时传递这些参数,还是可以通过命令提示符运行我的脚本并仍能绘制图表?
我正在使用Windows机器.如果您需要更多信息,请告诉我.就此而言.
解决方法
class Args: test_name = '''' symbolSet = ''defaultOne'' args=Args()
还有一个自动转换脚本.
http://35.192.144.192:8000/arg2cls.html
html – 如何更改ipython笔记本中的字体
作为参考,这些是回答以下链接的SO问题:
> in #1:一个未命名的文件在/usr/lib/python2.6/…/css/
> in comment to #1:在浏览器中更改等宽字体 – 工作,但字体是斜体
> in #2:custom.css in profile子目录/static/custom/custom.css
相关问题:
> Change ipython notebook font type
> Change font & background color in ipython notebook
> Changing (back to default) font in ipython notebook(未回覆) –
编辑:
根据#1的回答评论中的建议,更改浏览器中的等宽字体工作.然而字体是斜体的,这不是什么意思.
解决方法
然后,在.ipython中,您将看到默认的profile_default目录.该目录将具有static / custom / custom.css文件.
您现在可以将更改应用于此custom.css文件.您可以使用或搜索的custom.css文件中有很多样式.例如,你可以看到this link(这是我自己定制的custom.css文件)
基本上,此custom.css文件将更改应用于浏览器.您可以在ipython笔记本中使用inspect元素来查看要更改的元素.然后,您可以更改为custom.css文件.例如,您可以添加这些块来更改.CodeMirror pre中的字体以键入Monaco
.CodeMirror pre {font-family:Monaco; font-size:9pt;}
IPtthon笔记本在plt.show()之后停止评估单元格
这是我用过的代码.
Cell Toolbar: Question 1: Rotational Invariance of PCA I(1): Importing the data sets and plotting a scatter plot of the two. In [1]: # Channging the working directory import os os.getcwd() path="/Users/file/" os.chdir(path) pwd=os.getcwd() print(pwd) # Importing the libraries import pandas as pd import numpy as np import scipy as sp # Mentioning the files to be imported file=["2d-gaussian.csv","2d-gaussian-rotated.csv"] # Importing the two csv files in pandas dataframes XI=pd.read_csv(file[0],header=None) XII=pd.read_csv(file[1],header=None) #XI XII Out[5]: 0 1 0 1.372310 -2.111748 1 -0.397896 1.968246 2 0.336945 1.338646 3 1.983127 -2.462349 4 -0.846672 0.606716 5 0.582438 -0.645748 6 4.346416 -4.645564 7 0.830186 -0.599138 8 -2.460311 2.096945 9 -1.594642 2.828128 10 3.767641 -3.401645 11 0.455917 -0.224665 12 2.878315 -2.243932 13 -1.062223 0.142675 14 -0.698950 1.113589 15 -4.681619 4.289080 16 0.411498 -0.041293 17 0.276973 0.187699 18 1.500835 -0.284463 19 -0.387535 -0.265205 20 3.594708 -2.581400 21 2.263455 -2.660592 22 -1.686090 1.566998 23 1.381510 -0.944383 24 -0.085535 -1.697205 25 1.030609 -1.448967 26 3.647413 -3.322129 27 -3.474906 2.977695 28 -7.930797 8.506523 29 -0.931702 1.440784 ... ... ... 70 4.433750 -2.515612 71 1.495646 -0.058674 72 -0.928938 0.605706 73 -0.890883 -0.005911 74 -2.245630 1.333171 75 -0.707405 0.121334 76 0.675536 -0.822801 77 1.975917 -1.757632 78 -1.239322 2.053495 79 -2.360047 1.842387 80 2.436710 -1.445505 81 0.348497 -0.635207 82 -1.423243 -0.017132 83 0.881054 -1.823523 84 0.052809 1.505141 85 -2.466735 2.406453 86 -0.499472 0.970673 87 4.489547 -4.443907 88 -2.000164 4.125330 89 1.833832 -1.611077 90 -0.944030 0.771001 91 -1.677884 1.920365 92 0.372318 -0.474329 93 -2.073669 2.020200 94 -0.131636 -0.844568 95 -1.011576 1.718216 96 -1.017175 -0.005438 97 5.677248 -4.572855 98 2.179323 -1.704361 99 1.029635 -0.420458 100 rows × 2 columns The two raw csv files have been imported as data frames. Next we will concatenate both the dataframes into one dataframe to plot a combined scatter plot In [6]: # Joining two dataframes into one. df_combined=pd.concat([XI,XII],axis=1,ignore_index=True) df_combined Out[6]: 0 1 2 3 0 2.463601 -0.522861 1.372310 -2.111748 1 -1.673115 1.110405 -0.397896 1.968246 2 -0.708310 1.184822 0.336945 1.338646 3 3.143426 -0.338861 1.983127 -2.462349 4 -1.027700 -0.169674 -0.846672 0.606716 5 0.868458 -0.044767 0.582438 -0.645748 6 6.358290 -0.211529 4.346416 -4.645564 7 1.010685 0.163375 0.830186 -0.599138 8 -3.222466 -0.256939 -2.460311 2.096945 9 -3.127371 0.872207 -1.594642 2.828128 10 5.069451 0.258798 3.767641 -3.401645 11 0.481244 0.163520 0.455917 -0.224665 12 3.621976 0.448577 2.878315 -2.243932 13 -0.851991 -0.650218 -1.062223 0.142675 14 -1.281659 0.293194 -0.698950 1.113589 15 -6.343242 -0.277567 -4.681619 4.289080 16 0.320172 0.261774 0.411498 -0.041293 17 0.063126 0.328573 0.276973 0.187699 18 1.262396 0.860105 1.500835 -0.284463 19 -0.086500 -0.461557 -0.387535 -0.265205 20 4.367168 0.716517 3.594708 -2.581400 21 3.481827 -0.280818 2.263455 -2.660592 22 -2.300280 -0.084211 -1.686090 1.566998 23 1.644655 0.309095 1.381510 -0.944383 24 1.139623 -1.260587 -0.085535 -1.697205 25 1.753325 -0.295824 1.030609 -1.448967 26 4.928210 0.230011 3.647413 -3.322129 27 -4.562678 -0.351581 -3.474906 2.977695 28 -11.622940 0.407100 -7.930797 8.506523 29 -1.677601 0.359976 -0.931702 1.440784 ... ... ... ... ... 70 4.913941 1.356329 4.433750 -2.515612 71 1.099070 1.016093 1.495646 -0.058674 72 -1.085156 -0.228560 -0.928938 0.605706 73 -0.625769 -0.634129 -0.890883 -0.005911 74 -2.530594 -0.645206 -2.245630 1.333171 75 -0.586007 -0.414415 -0.707405 0.121334 76 1.059484 -0.104132 0.675536 -0.822801 77 2.640018 0.154351 1.975917 -1.757632 78 -2.328373 0.575707 -1.239322 2.053495 79 -2.971570 -0.366041 -2.360047 1.842387 80 2.745141 0.700888 2.436710 -1.445505 81 0.695584 -0.202735 0.348497 -0.635207 82 -0.994271 -1.018499 -1.423243 -0.017132 83 1.912425 -0.666426 0.881054 -1.823523 84 -1.026954 1.101637 0.052809 1.505141 85 -3.445865 -0.042626 -2.466735 2.406453 86 -1.039549 0.333189 -0.499472 0.970673 87 6.316906 0.032272 4.489547 -4.443907 88 -4.331379 1.502719 -2.000164 4.125330 89 2.435918 0.157511 1.833832 -1.611077 90 -1.212710 -0.122350 -0.944030 0.771001 91 -2.544347 0.171460 -1.677884 1.920365 92 0.598670 -0.072133 0.372318 -0.474329 93 -2.894802 -0.037809 -2.073669 2.020200 94 0.504119 -0.690281 -0.131636 -0.844568 95 -1.930254 0.499670 -1.011576 1.718216 96 -0.715406 -0.723096 -1.017175 -0.005438 97 7.247917 0.780923 5.677248 -4.572855 98 2.746180 0.335849 2.179323 -1.704361 99 1.025371 0.430754 1.029635 -0.420458 100 rows × 4 columns Plotting two separate scatter plot of all the four columns onto one scatter diagram In [ ]: import matplotlib.pyplot as plt # Fucntion for scatter plot def scatter_plot(): # plots scatter for first two columns(Unrotated Gaussian data) plt.scatter(df_combined.ix[:,0],df_combined.ix[:,1],color='red',marker='+') # plots scatter for Rotated Gaussian data plt.scatter(df_combined.ix[:,2],3],color='green',marker='x') legend = plt.legend(loc='upper right') # set ranges of x and y axes plt.xlim([-12,12]) plt.ylim([-12,12]) plt.show() # Function call scatter_plot() In [ ]: def plot_me1(): # create figure and axes fig = plt.figure() # split the page into a 1x1 array of subplots and put me in the first one (111) # (as a matter of fact,the only one) ax = fig.add_subplot(111) # plots scatter for x,y1 ax.scatter(df_combined.ix[:,marker='+',s=100) # plots scatter for x,y2 ax.scatter(df_combined.ix[:,marker='x',s=100) plt.xlim([-12,12]) plt.show() plot_me1() In [ ]:
解决方法
而是使用%matplotlib内联或酷新的%matplotlib笔记本开始你的笔记本(后者只能使用matplotlib> = 1.4.3和ipython> = 3.0)
在评估每个单元格后,(仍然打开的)图形对象会自动显示在您的笔记本中.
这个最小的代码示例适用于笔记本.请注意,它不会调用plt.show()
%matplotlib inline import matplotlib.pyplot as plt x = [1,2,3] y = [3,1] _ = plt.plot(x,y)
%matplotlib内联只显示图像.
最近添加了%matplotlib笔记本,并提供了交互式后端的许多很酷的功能(缩放,测量……):
ipython-notebook – iPython笔记本不支持VPN
Astrill Error Connection Closed Gracefully
如果VPN正在运行,iPython笔记本也不会让我打开或保存工作簿.知道为什么以及如何解决这个问题?
解决方法
whitelist means an address list of direct access,which your VPN would not transfer the corresponding traffic to the proxy address.
今天关于在ipython笔记本中读取单元格内容和macpython读取txt文件的分享就到这里,希望大家有所收获,若想了解更多关于anaconda – 如何在IPython jupyter笔记本中传递命令行参数、html – 如何更改ipython笔记本中的字体、IPtthon笔记本在plt.show()之后停止评估单元格、ipython-notebook – iPython笔记本不支持VPN等相关知识,可以在本站进行查询。
本文标签: