好吧这是我的最后一个问题,所以我终于找到一个打印好的api,但是我的问题是我得到错误,如果有人可以为我看这个并告诉我什么是错的那将是伟大的
import urllib
import json
request = urlopen("http://api.exmaple.com/stuff?client_id=someid&client_secret=randomsecret")
response = request.read()
json = json.loads(response)
if json['success']:
ob = json['response']['ob']
print ("The current weather in Seattle is %s with a temperature of %d") % (ob['weather'].lower(), ob['tempF'])
else:
print ("An error occurred: %s") % (json['error']['description'])
request.close()
Run Code Online (Sandbox Code Playgroud)
这是错误
Traceback (most recent call last):
File "thing.py", line 4, in <module>
request = urlopen("http://api.exmaple.com/stuff?client_id=someid&client_secret=randomsecret")
NameError: name 'urlopen' is not defined
Run Code Online (Sandbox Code Playgroud) 当我进入
username = str(input("Username:"))
password = str(input("Password:"))
运行程序后,我用我的名字输入输入并按回车键
username = sarang
我收到了错误
NameError: name 'sarang' is not defined
我试过了
username = '"{}"'.format(input("Username:"))
和
password = '"{}"'.format(input("Password:"))
但我最终得到了同样的错误.
如何将输入转换为字符串并修复错误?
我正在关注https://blog.patricktriest.com/analyzing-cryptocurrencies-python/ 上的教程,但我有点卡住了。我很想定义一个函数,然后立即调用。
我的代码如下:
def merge_dfs_on_column(dataframes, labels, col):
'''merge a single column of each dataframe on to a new combined dataframe'''
series_dict={}
for index in range(len(dataframes)):
series_dict[labels[index]]=dataframes[index][col]
return pd.DataFrame(series_dict)
# Merge the BTC price dataseries into a single dataframe
btc_usd_datasets= merge_dfs_on_column(list(exchange_data.values()),list(exchange_data.keys()),'Weighted Price')
Run Code Online (Sandbox Code Playgroud)
我可以清楚地看到我已经定义了merge_dfs_on_column fucntion,并且我认为语法是正确的,但是,当我在最后一行调用该函数时,出现以下错误:
NameError Traceback (most recent call last)
<ipython-input-22-a113142205e3> in <module>()
1 # Merge the BTC price dataseries into a single dataframe
----> 2 btc_usd_datasets= merge_dfs_on_column(list(exchange_data.values()),list(exchange_data.keys()),'Weighted Price')
NameError: name 'merge_dfs_on_column' is not defined
Run Code Online (Sandbox Code Playgroud)
我在谷歌上搜索了答案并仔细检查了语法,但我不明白为什么在调用时无法识别该函数。
无法在 pwntools 中设置进程Python 2.7.17。
源代码:
from pwn import *
s=process('/root/Dokumente/Scripts/example_program')
Run Code Online (Sandbox Code Playgroud)
我试过from pwn import *:
root@bitpc:~# python pwn.py
Traceback (most recent call last):
File "pwn.py", line 1, in <module>
from pwn import *
File "/root/pwn.py", line 2, in <module>
s=process('/root/Dokumente/Scripts/example_program')
NameError: name 'process' is not defined
Run Code Online (Sandbox Code Playgroud)
那是行不通的。然后我直接导入流程:
root@bitpc:~# python pwn.py
Traceback (most recent call last):
File "pwn.py", line 1, in <module>
from pwn import process
File "/root/pwn.py", line 1, in <module>
from pwn import process
ImportError: cannot …Run Code Online (Sandbox Code Playgroud) 我正在尝试捕获无法加载模块时发生的任何异常.目前的结果是"except"块没有被执行.
import sys
def loadModule(module):
try:
import module
except:
print """
Cannot load %s
For this script you will need:
cx_Oracle: http://cx-oracle.sourceforge.net/
pycrypto: https://www.dlitz.net/software/pycrypto/
paramiko: http://www.lag.net/paramiko/
""" % module
sys.exit(1)
loadModule(cx_Oracle)
Run Code Online (Sandbox Code Playgroud)
错误:
Traceback (most recent call last):
File "./temp_script.py", line 16, in <module>
loadModule(cx_Oracle)
NameError: name 'cx_Oracle' is not defined
Run Code Online (Sandbox Code Playgroud) import pickle
import os
import time
class Person():
def __init__(self, number, address):
self.number = number
self.address = address
def save():
with open('mydict.pickle', 'wb') as f:
pickle.dump(mydict, f)
mydict = {}
mydict['Avi'] = ['347-000-0000', 'Oceanview']
mydict['Alan'] = ['347-000-0000', 'Brighton']
mydict['Frank'] = ['718-000-0000', 'Brighton']
print('add a name to the database.')
name = input('Name:')
number = input('Digits:')
address = input('Address:')
mydict[name] = [number, address]
-------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
错误:如果我尝试向数据库添加名称,我会得到一个名称错误.NameError:未定义名称'alan'.奇怪的是,字符串不起作用,但数字会起作用.对不起,如果我的问题不清楚.
Traceback (most recent call last):
File "C:/Python33/ss", line 21, in <module>
name = input('Name:')
File "<string>", line …Run Code Online (Sandbox Code Playgroud) 我有一个蜘蛛文件dmoz_spider.py,它的对象是:
from scrapy.spider import Spider
from scrapy.selector import Selector
from dmoz.items import DmozItem
class DmozSpider(Spider):
name = "dmoz"
allowed_domains = ["m.timesofindia.com"]
start_urls = ["http://m.timesofindia.com/india/Congress-BJP-spar-over-Gujarat-govts-Rs-11-per-day-poverty-line/articleshow/29830237.cms"]
def parse(self, response):
sel = Selector(response)
torrent = DmozItem()
filename = response.url.split("/")[-2]+"1.txt"
torrent['link'] = response.url
torrent['title'] = sel.xpath("//h1/text()").extract()
open(filename, 'wb').write(torrent['link'])
Run Code Online (Sandbox Code Playgroud)
第二个文件是items.py
from scrapy.item import Item, Field
class DmozItem(Item):
title = Field()
link = Field()
desc = Field()
Run Code Online (Sandbox Code Playgroud)
当我运行我的爬虫时,我在命令行上遇到以下错误...
ImportError:没有名为dmoz.items的模块
至于什么时候我从我的蜘蛛文件中删除了import语句,它给了我错误的说法
exceptions.NameError:未定义全局名称'DmozItem'
以下代码在IDLE中运行正常,但我得到"NameError:全局名称'messagebox'未定义".但是,如果我明确说明from tkinter import messagebox,它可以从任何地方运行.
from tkinter import *
from tkinter import ttk
root = Tk()
mainFrame = ttk.Frame(root)
messagebox.showinfo("My title", "My message", icon="warning", parent=mainFrame)
Run Code Online (Sandbox Code Playgroud)
为什么IDLE不需要显式的import语句,但在其他地方需要它?
这是我的代码:
import os
if os.path.exists(r'C:\Genisis_AI'):
print("Main File path exists! Continuing with startup")
else:
createDirs()
def createDirs():
os.makedirs(r'C:\Genisis_AI\memories')
Run Code Online (Sandbox Code Playgroud)
当我执行它时,它会抛出一个错误:
File "foo.py", line 6, in <module>
createDirs()
NameError: name 'createDirs' is not defined
Run Code Online (Sandbox Code Playgroud)
我确定它不是拼写错误,我没有拼错函数的名称,为什么我得到一个NameError?
我想用eval做操作来查看是否a和b等于c某个运算符。
我的代码:
def Expression(a, b, c):
operators = ["+", "-", "*", "/"]
return len([i for i in operators if eval("a () b".replace("()", i)) == c]) >= 1
Expression(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
出于某种原因,这会导致 NameError。错误日志:
return len([i for i in operators if eval("a () b".replace("()", i)) == c]) >= 1
File "<string>", line 1, in <module>
NameError: name 'a' is not defined
Run Code Online (Sandbox Code Playgroud)
由于该函数a作为参数,我认为a不应未定义。这里有什么问题?
nameerror ×10
python ×8
function ×2
import ×2
python-2.7 ×2
python-3.x ×2
addressbook ×1
eval ×1
importerror ×1
messagebox ×1
module ×1
pwntools ×1
scrapy ×1
string ×1
tkinter ×1
user-input ×1