我的理解是python将打印repr输出,但显然并非总是如此.例如:
在ipython中:
In [1]: type([])
Out[1]: list
In [2]: set([3,1,2])
Out[2]: {1, 2, 3}
Run Code Online (Sandbox Code Playgroud)
在python中:
>>> type([])
<type 'list'>
>>> set([3,1,2])
set([1, 2, 3])
Run Code Online (Sandbox Code Playgroud)
ipython在输出上应用了哪些转换?
我在fedora 18上成功运行ipython直到现在:我在尝试启动它时遇到以下异常:
Traceback (most recent call last):
File "/usr/bin/ipython", line 9, in <module>
load_entry_point('ipython==1.1.0', 'console_scripts', 'ipython')()
File "/usr/lib/python2.7/site-packages/IPython/__init__.py", line 118, in start_ipython
return launch_new_instance(argv=argv, **kwargs)
File "/usr/lib/python2.7/site-packages/IPython/config/application.py", line 544, in launch_instance
app.initialize(argv)
File "<string>", line 2, in initialize
File "/usr/lib/python2.7/site-packages/IPython/config/application.py", line 89, in catch_config_error
return method(app, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 323, in initialize
self.init_shell()
File "/usr/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 339, in init_shell
ipython_dir=self.ipython_dir, user_ns=self.user_ns)
File "/usr/lib/python2.7/site-packages/IPython/config/configurable.py", line 349, in instance
inst = cls(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/IPython/terminal/interactiveshell.py", line 320, in __init__
**kwargs …Run Code Online (Sandbox Code Playgroud) 所以 - ROOT社区中的优秀人员产生了以下魔力:
# This is for intercepting the output of ROOT
# In a cell, put %%rootprint so that the output that would normally be
# sent directly to the stdout will instead be displayed in the cell.
# It must be the first element in the cell.
import tempfile
import ROOT
from IPython.core.magic import (Magics, magics_class, cell_magic)
@magics_class
class RootMagics(Magics):
"""Magics related to Root.
%%rootprint - Capture Root stdout output and show in result cell
"""
def __init__(self, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用PyPyODBC,特别是对这个例子的看法.但是,我对创建一个新表并不感兴趣,我只是想读取数据(它驻留在SQL Server中的数据库/表中)并在Python中使用它.
下面的代码工作正常,但以下部分(
for row in cur.fetchall():)除外.
from __future__ import print_function
import pypyodbc
from pandas import *
import pandas as pd
con = pypyodbc.connect(driver = '{SQL Server}', server = 'WIN-SAV35R5AKCQ',database ='V2.0.6_X12_837I')
cur = con.cursor()
cur.execute('''SELECT * FROM TS837_2400;''')
cur.commit()
for d in cur.description:
print (d[0], end=" ")
print('')
for row in cur.fetchall():
for field in row:
print (field, end=" ")
print ('')
cur.close()
con.close()
Run Code Online (Sandbox Code Playgroud)
但是,我收到此错误.
---------------------------------------------------------------------------
Error Traceback (most recent call last)
<ipython-input-70-c08d5a03056a> in <module>() …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法来测试一个元组是否具有所有不同的元素 - 所以说,它是一个集合,最终得到了这个快速而肮脏的解决方案.
def distinct ( tup):
n=0
for t in tup:
for k in tup:
#print t,k,n
if (t == k ):
n = n+1
if ( n != len(tup)):
return False
else :
return True
print distinct((1,3,2,10))
print distinct((3,3,4,2,7))
Run Code Online (Sandbox Code Playgroud)
有什么想法错误?是否内置了元组?
在学习Python的过程中,这段代码在Spyder中运行良好,但在iPython中有所突破:
# Create a sequence
sequence = ['A', 'B', 'C', 'D']
# Create two variables, the first with 'A', the second with the rest of the letters
Alpha, *allOtherLetters = sequence
Run Code Online (Sandbox Code Playgroud)
当我尝试在iPython中运行它时,我收到此错误:
File "<ipython-input-3-b15e0241bc60>", line 5
Alpha, *allOtherLetters = sequence
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
显然iPython不喜欢*,但我不知道该怎么做.
我想检查列表phrases中的任何字符串元素是否包含集合中的某些关键字phd_words.我想使用,any但它不起作用.
In[19]:
import pandas as pd
import psycopg2 as pg
def test():
phd_words = set(['doctor', 'phd'])
phrases = ['master of science','mechanical engineering']
for word in phrases:
if any(keyword in word for keyword in phd_words):
return 'bingo!'
test()
Out[20]:
bingo!
Run Code Online (Sandbox Code Playgroud)
我该怎么解决这个问题?
我有一个csv文件准备加载到我的python代码,但是,我想加载到以下格式:
data = [[A,B,C,D],
[A,B,C,D],
[A,B,C,D],
]
Run Code Online (Sandbox Code Playgroud)
我如何加载一个可读作为numpy数组的.csv文件?例如,简单地使用以前的教程会破坏使用:
data = np.array(data)
Run Code Online (Sandbox Code Playgroud)
如果做不到这一点,我只想上传我的csv文件(例如'dual-Cored.csv'作为data = dual-Cored.csv)
我正在使用iPython来控制连接到Mac的其他设备.大部分时间它运行正常,但有时我给设备发出了错误的命令,iPython只是永远挂起,因为它正在等待响应但不会得到一个,所以我需要一种方法来杀死iPython进程.ctrl+c什么都不做,它只是^C在屏幕上打印一个字符串.ctrl+z可以让我离开iPython,但它似乎没有杀死它,因为当我重新启动iPython时,我无法重新建立与设备的通信.最终我不得不重新启动计算机让两个人再次交谈.
我正在尝试根据此处的教程使用plotly:http ://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s00_homepage/s00_homepage.ipynb
似乎具有正确的库和Python版本,尽管在密谋导入时出现AttributeError。想知道是否有人遇到相同的问题,请参见下面的版本和stacktrace:
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
Type "copyright", "credits" or "license" for more information.
IPython 2.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import numpy
In [2]: numpy.__version__
Out[2]: '1.8.1'
In [3]: import pandas
In [4]: pandas.__version__
Out[4]: '0.13.1'
In [5]: import plotly
---------------------------------------------------------------------------
AttributeError Traceback (most …Run Code Online (Sandbox Code Playgroud) ipython ×10
python ×8
csv ×1
distinct ×1
if-statement ×1
io ×1
macos ×1
matplotlib ×1
numpy ×1
plot ×1
plotly ×1
pypyodbc ×1
python-3.x ×1
repr ×1
set ×1
sql-server ×1
tuples ×1