我的同事需要我从我的python timestamp对象中删除毫秒,以符合旧的POSIX(IEEE Std 1003.1-1988)标准.为我完成这项任务的折磨路线如下:
datetime.datetime.strptime(datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S"),"%Y-%m-%d %H:%M:%S")
Run Code Online (Sandbox Code Playgroud)
有没有比这更简单的方法来为mongoDB结束datetime.datetime对象?
Python 在 python 3.4 到 3.7 中将其 pickle 协议更改为 4,并在 python 3.8 中再次将其更改为 protocol=5。如何在 python 3.8 中打开旧的 pickled 文件?
我试过:
>>> with open('data_frame_111.pkl','rb') as pfile:
... x1 = pickle.load(pfile)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
AttributeError: Can't get attribute 'new_block' on <module
'pandas.core.internals.blocks' from '/opt/anaconda3/lib/python3.8/site-
packages/pandas/core/internals/blocks.py'>
Run Code Online (Sandbox Code Playgroud)
和
>>> with open('data_frame_111.pkl','rb') as pfile:
... x1 = unpkl.load(pfile, protocol=4)
Run Code Online (Sandbox Code Playgroud)
但虽然协议是一个关键字,但pickle.dump它不是pickle.load. 实例化pickle.Unpickler()也不起作用。但显然应该有办法。
在 python 3.7 中,我会import pickle5使用它来打开较新的 pickles,但找不到在 python …
textblob中的内置分类器非常笨。它是根据电影评论进行训练的,所以我在上下文中创建了大量示例(57,000个故事,分为正面或负面故事),然后使用nltk.尝试使用textblob对其进行训练的方式进行了训练,但始终失败:
with open('train.json', 'r') as fp:
cl = NaiveBayesClassifier(fp, format="json")
Run Code Online (Sandbox Code Playgroud)
那将运行数小时,并最终导致内存错误。
我查看了源代码,发现它只是使用nltk并将其包装起来,所以我改用了它,并且可以正常工作。
nltk训练集的结构必须是一个元组列表,其中第一部分是文本中的单词计数器和出现频率。元组的第二部分是“ pos”或“ neg”。
>>> train_set = [(Counter(i["text"].split()),i["label"]) for i in data[200:]]
>>> test_set = [(Counter(i["text"].split()),i["label"]) for i in data[:200]] # withholding 200 examples for testing later
>>> cl = nltk.NaiveBayesClassifier.train(train_set) # <-- this is the same thing textblob was using
>>> print("Classifier accuracy percent:",(nltk.classify.accuracy(cl, test_set))*100)
('Classifier accuracy percent:', 66.5)
>>>>cl.show_most_informative_features(75)
Run Code Online (Sandbox Code Playgroud)
然后我腌了。
with open('storybayes.pickle','wb') as f:
pickle.dump(cl,f)
Run Code Online (Sandbox Code Playgroud)
现在...我拿了这个腌制的文件,然后重新打开它以获取nltk.classifier'nltk.classify.naivebayes.NaiveBayesClassifier'>-并尝试将其输入到textblob中。代替
from textblob.classifiers import NaiveBayesClassifier
blob = TextBlob("I love this …Run Code Online (Sandbox Code Playgroud) 我创建了一个需要谷歌范围的应用程序,它一直在工作.我收到此错误:
Error
ERROR:Scope has changed from "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://mail.google.com" to "https://mail.google.com/ https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile".
Run Code Online (Sandbox Code Playgroud)
"https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/userinfo.profile
https://mail.google.com"
Run Code Online (Sandbox Code Playgroud)
"https://mail.google.com/
https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/userinfo.profile"
Run Code Online (Sandbox Code Playgroud)
据我所知,范围没有改变.之前和之后有3个范围,但只有订单已更改.
这里的python代码在这里:
try:
credentials = oauth.fetch_token('https://accounts.google.com/o/oauth2/token',
authorization_response = full_authorization_response_url,
client_secret=client_secret)
except Exception as e:
import traceback
print(traceback.format_exc())
credentials = 'ERROR:'+str(e)
if type(credentials) in (str,unicode):
return "Error<br>"+credentials
Run Code Online (Sandbox Code Playgroud)
这是最后一行似乎是相关的.所以谷歌说范围发生了变化,但我无法理解为什么,或者如何解决它,因为它没有改变.
我正在使用 MacOS,系统配置中的某些内容不断告诉 pythonSSHTunnelForwarder使用我的默认id_rsa文件,而不是我在下面的配置中指定的文件:
>>> from sshtunnel import SSHTunnelForwarder
>>> db_tunnel = SSHTunnelForwarder(
... ssh_host="localhost",
... ssh_username="username",
... ssh_port=1111,
... ssh_pkey="~/.ssh/test",
... remote_bind_address=("my-remote-database-domain", 3306)
... )
Run Code Online (Sandbox Code Playgroud)
给我这个错误消息:
2022-03-23 13:15:35,715| ERROR | Password is required for key /Users/me/.ssh/id_rsa
我可以在哪里编辑配置以覆盖对错误密钥的搜索?
我的~/.ssh/config:
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
IdentityFile ~/.ssh/test
Run Code Online (Sandbox Code Playgroud)
是的,我已经跑去ssh-add ~/.ssh/test添加这个密钥了。
还有什么能让它感到困惑呢?
我花了一个小时试图挖掘 MongoDB 的在线文档,当用户管理命令不断变化时,该文档对于 2.4 版本来说相当糟糕。
我在 mongoDB 2.4.11 中发现了这一点
db.addUser( { user: "...", pwd: "...", roles: [ ... ] } )
Run Code Online (Sandbox Code Playgroud)
作品。但我找不到任何更新用户角色的命令示例,并且当我尝试覆盖现有用户时收到此错误消息:
User already exists with that username/userSource combination at src/mongo/shell/db.js:125
Run Code Online (Sandbox Code Playgroud)
所以我被困住了。
(作为奖励,我如何在 2.4 中从现有用户中删除角色?)
当我尝试时
db.grantRolesToUser( "myself", [ {role: "clusterAdmin"} ] )
Run Code Online (Sandbox Code Playgroud)
如 v2.6 中所述,我得到:
TypeError: Property 'grantRolesToUser' of object admin is not a function
Run Code Online (Sandbox Code Playgroud)
所以在 2.4 中不起作用。
我正在学习lxml(在使用ElementTree之后),我很困惑为什么.fromstring和.tostring似乎不可逆.这是我的例子:
import lxml.etree as ET
f = open('somefile.xml','r')
data = f.read()
tree_in = ET.fromstring(data)
tree_out = ET.tostring(tree_in)
f2 = open('samefile.xml','w')
f2.write(tree_out)
f2.close
Run Code Online (Sandbox Code Playgroud)
'somefile.xml'是132 KB.'samefile.xml' - 输出 - 是113 KB,它在某个arbirtrary点缺少文件的结尾.整个树的结束标记和最终元素的一些部分刚刚消失.
我的代码有问题,或者原始XML文件中的嵌套是否有问题?如果是这样,我是否被迫再次使用ElementTree的BeautifulSoup(没有xpath)?
一个注意事项:许多元素中的文本都有一堆被转换为文本的废话,但这是什么导致了这个问题?
例:
<QuestionIndex Id="Perm"><Answer><![CDATA[confirm]]></Answer><Answer><![CDATA[NotConfirm]]></Answer></QuestionIndex>
<QuestionIndex Id="Actor"><Answer><![CDATA[GirlLt16]]></Answer><Answer><![CDATA[Fem17to25]]></Answer><Answer><![CDATA[BoyLt16]]></Answer><Answer><![CDATA[Mal17to25]]></Answer><Answer><![CDATA[Moth]]></Answer><Answer><![CDATA[Fath]]></Answer><Answer><![CDATA[Elder]]></Answer><Answer><![CDATA[RelLead]]></Answer><Answer><![CDATA[Auth]]></Answer><Answer><![CDATA[Teach]]></Answer><Answer><![CDATA[Oth]]></Answer></QuestionIndex>
Run Code Online (Sandbox Code Playgroud) python networkx模块有一个方法nx.union,用于合并两个网络映射:
C = nx.union(G,H)
Run Code Online (Sandbox Code Playgroud)
其中G和H是网络地图,而C是组合版本.它按编号查找所有节点,并使用相同的编号合并节点.
以GEXF格式存储的节点示例:
<node id="0" label="walking">
<ns0:color b="200" g="11" r="11" />
<attvalues>
<attvalue for="0" value="2" />
<attvalue for="1" value="26" />
</attvalues>
</node>
Run Code Online (Sandbox Code Playgroud)
这对我来说是个问题,因为我的地图有任意节点号,但每个节点属性都有唯一的标签.当我分配节点号时,我只是通过一个列表并使用列表索引作为节点号,但map1可能在位置84处"行走"而map2可能在位置157处"行走" - 因此节点号不容易与节点标签相关.
有谁知道我应该如何使用networkx联合网络地图并指定节点标签名称的匹配?
我可能必须编写自己的方法,但只是想确定我没有遗漏一些东西.有一个union_disjoint()但似乎在同一个文件中创建了两个单独的未连接映射,这没有用.
第二个networkx问题:有没有办法在GEXF文件格式中指定节点标签大小和标签颜色?未列入GEXF v1.2草案规范中.
python ×5
datetime ×1
google-oauth ×1
lxml ×1
mongodb ×1
naivebayes ×1
networkx ×1
nltk ×1
pickle ×1
python-3.8 ×1
python-3.x ×1
ssh-tunnel ×1
textblob ×1
tostring ×1