k = 'a bunch of data and then name ""Serpin-ps""'
print re.search(r'name\s""(\w+)""',k).group(1)
Run Code Online (Sandbox Code Playgroud)
得到:
AttributeError: 'NoneType' object has no attribute 'group'
Run Code Online (Sandbox Code Playgroud)
desired_output = 'Serpin-ps'
有道理,因为文本中有" - ".
反正有没有让正则表达式将' - '与所有其他字母数字字符结合起来?
我正在尝试从一个数据库中的表中提取架构并将其传输到另一个数据库。以下是执行此操作的函数的一部分。Table( ... )调用时会出错。
功能示例:
def transfer_data(self, from_db, to_db, tables, flush_interval=1000, table_data_filters = {}):
if from_db is to_db or from_db == to_db:
raise Exception("""Usage: Can't transfer tables to and from the same database!""")
source, sengine = (from_db.new_session(), from_db.engine()) if isinstance(from_db, SessionManager) else self.__make_session(from_db)
dest, dengine = (to_db.new_session(), to_db.engine()) if isinstance(to_db, SessionManager) else self.__make_session(to_db)
meta = MetaData()
with source.no_autoflush and dest.no_autoflush:
for table_name in tables:
print 'Processing', table_name
print 'Pulling schema from source server'
# ERROR OCCURS BELOW WITH CALL …Run Code Online (Sandbox Code Playgroud) 我在我的MacOSX Mavericks Macbook Pro(python 2.7)上安装了Stripe
但我总是收到以下错误消息:
$ sudo python stripe.py
Traceback (most recent call last):
File "stripe.py", line 1, in <module>
import stripe
File "/Users/sebastian/Desktop/stripe.py", line 7, in <module>
resp = stripe.Charge.create(
AttributeError: 'module' object has no attribute 'Charge'
Run Code Online (Sandbox Code Playgroud)
当我尝试执行以下脚本时:
import stripe
stripe.api_key = 'my_test_secret_key'
resp = stripe.Charge.create(
amount=200,
currency='usd',
card={
'number': '4242424242424242',
'exp_month': 10,
'exp_year': 2014
},
description='customer@gmail.com'
)
Run Code Online (Sandbox Code Playgroud) 我有一个返回网页标题的小脚本
title = BeautifulSoup(urllib2.urlopen(URL)).title.string
Run Code Online (Sandbox Code Playgroud)
但并非所有网站都指定<title>标签,在这种情况下我的脚本会返回
AttributeError: 'NoneType' object has no attribute 'string'
Run Code Online (Sandbox Code Playgroud)
title如果网页上有标题,有没有办法让变量等于标题?
我经受过考验
if BeautifulSoup(urllib2.urlopen(url)).title.string.strip():
print BeautifulSoup(urllib2.urlopen(url)).title.string.strip()
else:
print url
Run Code Online (Sandbox Code Playgroud)
但它仍然会引发 AttributeError 错误
我正在尝试使用以下代码在目录中仅压缩 *.csv 文件:
allFiles = os.listdir( dirName + apt + '/' )
csvList = [i for i in allFiles if i.endswith('.csv')]
zf = zipfile.ZipFile([ dirName + apt + '.zip' ], mode='w')
for f in csvList:
a = dirName + apt + '/' + f
zf.write( a )
#all the elements of a are strings
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Traceback (most recent call last):
File "<ipython-input-43-ebf4dc807b56>", line 1, in <module>
zf.write(a)
File "C:\Users\blevy\MCR\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\zipfile.py", line 1347, in write
zinfo.header_offset = self.fp.tell() # Start of header …Run Code Online (Sandbox Code Playgroud) 在我的 python 代码中,该root.mainloop()行导致错误:
AttributeError: '_tkinter.tkapp' object has no attribute 'mainLoop'
我在 Mac 上,我的代码是:
from tkinter import *
root = Tk()
myLabel1 = Label(root, text = 'My First GUI')
myLabel1.pack()
root.mainLoop()
Run Code Online (Sandbox Code Playgroud) 我试图计算pandas DataFrame中的关键字数量:
df = pd.read_csv('amazon_baby.csv')
selected_words = ['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']
Run Code Online (Sandbox Code Playgroud)
selected_words必须从系列计算:df ['review']
我试过了
def word_counter(sent):
a={}
for word in selected_words:
a[word] = sent.count(word)
return a
Run Code Online (Sandbox Code Playgroud)
然后
df['totalwords'] = df.review.str.split()
df['word_count'] = df.totalwords.apply(word_counter)
----------------------------------------------------------------------------
----> 1 df['word_count'] = df.totalwords.apply(word_counter)
c:\users\admin\appdata\local\programs\python\python36\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
3192 else:
3193 values = self.astype(object).values
-> 3194 mapped = lib.map_infer(values, f, convert=convert_dtype)
3195
3196 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/src\inference.pyx in pandas._libs.lib.map_infer()
<ipython-input-51-cd11c5eb1f40> in word_counter(sent)
2 …Run Code Online (Sandbox Code Playgroud) 我正在使用克林特·哈里斯的解决方案
import fiona
import shapely
with fiona.open("./areas_shp.shp") as fiona_collection:
shapefile_record = next(iter(fiona_collection))
shape = shapely.geometry.asShape( shapefile_record['geometry'] )
point = shapely.geometry.Point(coords[0])
for point in coords:
if (shape.contains(point)):
print("yay")
Run Code Online (Sandbox Code Playgroud)
在这里,我只是用 shapefile 测试一个坐标,但看起来代码可能已经过时了。我已经更改shapefile_record = fiona_collection.next()为shapefile_record = next(iter(fiona_collection)),但这个错误我似乎无法解决:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-18ca8979d01f> in <module>
38 with fiona.open("./areas_shp.shp") as fiona_collection:
39 shapefile_record = next(iter(fiona_collection))
---> 40 shape = shapely.geometry.asShape( shapefile_record['geometry'] )
41 point = shapely.geometry.Point(coords[0])
42 for point in coords:
AttributeError: module 'shapely' has no attribute …Run Code Online (Sandbox Code Playgroud) 我有一个基于工人阶级的观点。但是在添加@login_required 时出现错误:
AttributeError: 'function' 对象没有属性 'as_view'
这里的 ResultListView 发生了一些事情:
from django.urls import path
from .views import ResultListView
urlpatterns = [
path('meetings/', ResultListView.as_view(), name='meetings'),
]
Run Code Online (Sandbox Code Playgroud)
我的意见.py:
@login_required
class ResultListView(ListView):
template_name = ...
def get_queryset(self):
return Result.objects.filter(rider__user=self.request.user)
Run Code Online (Sandbox Code Playgroud)
在我将装饰器放入之前,一切正常。现在非常困惑,我不明白为什么ResultListView在通过装饰器发送时应该失去它的属性。
我想运行此代码,但我不能并收到此错误。我也下载了熊猫包。
import pandas
data = {
"Day": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"Visitors": [18, 26, 18, 18, 9, 9, 20, 30, 16, 24],
"Bounce_Rate": [77.27, 74.07, 73.68, 65, 90, 70, 72, 62.16, 81.25, 72],
}
df = pandas.DataFrame(data)
print(df)
Run Code Online (Sandbox Code Playgroud) attributeerror ×10
python ×9
pandas ×2
decorator ×1
django ×1
list ×1
python-3.x ×1
regex ×1
replace ×1
search ×1
shapely ×1
sqlalchemy ×1
tell ×1
tkinter ×1
zip ×1