我有以下名为'data.csv'的文件:
1997,Ford,E350
1997, Ford , E350
1997,Ford,E350,"Super, luxurious truck"
1997,Ford,E350,"Super ""luxurious"" truck"
1997,Ford,E350," Super luxurious truck "
"1997",Ford,E350
1997,Ford,E350
2000,Mercury,Cougar
Run Code Online (Sandbox Code Playgroud)
我想将它解析为pandas DataFrame,以便DataFrame如下所示:
Year Make Model Description
0 1997 Ford E350 None
1 1997 Ford E350 None
2 1997 Ford E350 Super, luxurious truck
3 1997 Ford E350 Super "luxurious" truck
4 1997 Ford E350 Super luxurious truck
5 1997 Ford E350 None
6 1997 Ford E350 None
7 2000 Mercury Cougar None
Run Code Online (Sandbox Code Playgroud)
我能做的最好的事情是:
pd.read_table("data.csv", sep=r',', names=["Year", "Make", "Model", "Description"])
Run Code Online (Sandbox Code Playgroud)
哪个让我: …
我正在尝试win32core使用 pip 安装模块,但它不断显示有关包/轮元数据与文件名不匹配(版本不一致)的警告。
以下是下载旧版本之前命令提示符中显示的文本片段:
\npython -m pip install --no-cache-dir win32core\nCollecting win32core\n Downloading win32core-221.28.tar.gz (2.1 MB)\n |\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88| 2.1 MB 2.2 MB/s\n Installing build dependencies ... done\n Getting requirements to build wheel ... done\n Preparing wheel metadata ... done\nWARNING: Discarding https://files.pythonhosted.org/packages/24/\n0a6e0b2e201c76ccd3cda1f97bd64d45daa2b899b50e541962748ec5bc9f5d/\nwin32core-221.28.tar.gz#\nsha256=e805548ccde99a3f36931d3eef72cfaaa5dace4288393af898c96f92c1fe5f9e (from https://pypi.org/simple/win32core/).\nRequested win32core from https://files.pythonhosted.org/packages/\n24/0a/6e0b2e201c76ccd3cda1f97bd64d45daa2b899b50e541962748ec5bc9f5d/win32core-221.28.tar.gz#\nsha256=e805548ccde99a3f36931d3eef72cfaaa5dace4288393af898c96f92c1fe5f9e\nhas inconsistent version: filename has '221.28', but metadata has '0.0.0'\nRun Code Online (Sandbox Code Playgroud)\n我以前遇到过这种情况,我应该如何正确安装模块?(我使用的是Python 3.9.1)
\n我有一个字符串,我想使用python的.format函数在运行时添加一些变量,这是我的字符串:
'{"auth": {"tenantName": "{Insert String Here}", "passwordCredentials": {"username": "{insert String here}", "password": "{insert String Here}"}}}'
Run Code Online (Sandbox Code Playgroud)
当我像这样使用.format:
credentials='{"auth": {"tenantName": "{tenant}", "passwordCredentials": {"username": "{admin}", "password": "{password}"}}}'.format(tenant='me',admin='test',password='123')
Run Code Online (Sandbox Code Playgroud)
它给了我以下错误:
KeyError: '"auth"'
Run Code Online (Sandbox Code Playgroud)
任何帮助?提前致谢.
嗯.我似乎没有办法将Python的大整数存储在一个numpy数组中.你需要做些什么特别的事情来声明一个带有bigint的numpy数组吗?
我想通过以下方式动态导入模块:
我用这种结构创建了一个名为pkg的文件夹:
pkg
|__init__.py
|foo.py
Run Code Online (Sandbox Code Playgroud)
在头部__init__.py,添加此代码fragement:
pkgpath = os.path.dirname(pkg.__file__);
for module in pkgutil.iter_modules([pkgpath]):
__import__(module[1], locals(), globals());
m = sys.modules[module[1]];
print m.__package__;
Run Code Online (Sandbox Code Playgroud)
我发现m.__package__是None万一有在foo.py没有import语句,但如果我添加了一个简单的import语句是这样的:
import os
Run Code Online (Sandbox Code Playgroud)
那么m.__package__是"pkg"这是正确的包名.为什么会这样?
如何导入模块并确保其正确的包属性?
我有一个库类,它取决于它是如何导入的,一个依赖于self.__module__识别改变行为的方法 - 取决于我是相对还是绝对地导入它.有没有办法强制self.__name__类的属性绝对返回自己?
我意识到一个解决方案是强制每个人以相同的方式导入子类,但是想知道是否有办法从库的角度强制它.
总结结构
我在库中有一个模块
project/
mylib/
foo.py
LibraryClass
def get_name(self):
return "%s.%s.%s" % \
(self.__module__, self.__class__.__name__, self.some_init_property)
prog/
utils.py
fooClass(LibraryClass)
bar.py
def some_func()
#see below
Run Code Online (Sandbox Code Playgroud)
在mylib,我有一个导入器LibraryClass通过我们的django项目的设置文件中的类名字符串导入所有子类.
基本上,我们看到的行为取决于您的导入方式.所以我在bar.py中观察到两种行为:
def some_func_absolute():
from prog.utils import fooClass
f = fooClass("lalala")
print f.get_name()
#prints prog.utils.fooClass.lalala
Run Code Online (Sandbox Code Playgroud)
与
def some_func_relative():
from utils import fooClass
f = fooClass("lalala")
print f.get_name()
#prints foo.fooClass.lalala
Run Code Online (Sandbox Code Playgroud) 我有一个简单的Sphinx扩展如下:
from docutils import nodes
from docutils.parsers.rst import directives
from sphinx.util.compat import Directive
class testnode(nodes.Element):
def __init__(self, *args, **kwargs):
super(testnode, self).__init__(*args, **kwargs)
self['foo'] = '?'
def visit_testnode_latex(self, node):
self.body.append('Test: %s' % node['foo'])
def depart_testnode_latex(self, node):
pass
def visit_testnode_html(self, node):
self.body.append('<p>Test: %s</p>' % node['foo'])
def depart_testnode_html(self, node):
pass
class TestDirective(Directive):
has_content = False
required_arguments = 0
optional_arguments = 0
final_argument_whitespace = False
option_spec = {
'foo': directives.unchanged,
}
def run(self):
node = testnode()
node['foo'] = self.options.get('foo')
return [node]
def setup(app):
app.add_directive("testdirective", …Run Code Online (Sandbox Code Playgroud) import pandas
from sklearn import tree
import pydotplus
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
import matplotlib.image as pltimg
dtree = DecisionTreeClassifier()
dtree = dtree.fit(X, y)
y_pred = dtree.predict(X)
y_pred
Run Code Online (Sandbox Code Playgroud)
错误 ::
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-33-745bbfa9769b> in <module>
1 import pandas
----> 2 from sklearn import tree
3 import pydotplus
4 from sklearn.tree import DecisionTreeClassifier
5 import matplotlib.pyplot as plt
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/__init__.py in <module>
62 else:
63 from . import __check_build
---> 64 from .base import …Run Code Online (Sandbox Code Playgroud) 我想从python脚本中获取给定像素的物理天空坐标.我想使用astropy的WCS,但我会在python中做任何事情.
我试过这两段代码.
from astropy.io import fits
from astropy.wcs import WCS
def astropymethod1(img):
# from http://astropy.readthedocs.org/en/latest/wcs/
w = WCS(img)
lon, lat = w.all_pix2world( 100., 100., 1)
print lon, lat
def astropymethod2(img):
# from http://astropy.readthedocs.org/en/latest/wcs/
hdu = fits.open(img)
w = WCS(hdu[0].header)
lon, lat = w.wcs_pix2world(100., 100., 1)
print lon, lat
Run Code Online (Sandbox Code Playgroud)
问题是我第一次尝试使用WCS时出现错误,结果只是我输入的像素值.
WARNING: FITSFixedWarning: The WCS transformation has more axes (2) than the image it is associated with (0) [astropy.wcs.wcs]
Run Code Online (Sandbox Code Playgroud) 目前我有以下代码:
c.execute("SELECT * FROM table")
for row in c.fetchall():
print row[0]
print row[1]
Run Code Online (Sandbox Code Playgroud)
但是,我更改了表的结构,现在我必须更改索引值以表示此更改.有没有办法获得使用列名?