编辑18/02:由于我仍然没有解决方案,因此我将更新到目前为止的知识。
我已经成功安装了python 3.7。我可以使用pip(或pip3)安装模块,但这些模块已安装在Python 3.6中(随ubuntu一起提供)。因此,我无法在python 3.7中导入这些模块(找不到模块)Python 3.7无法识别pip / pip3,因此无法通过pip / pip3安装,我需要python 3.7
-
我已经在Ubuntu 18.04计算机上安装了Python 3.7。如果相关,请遵循以下说明:
下载:从Python网站[1]上的Python 3.7,在桌面上并手动解压缩,在桌面上安装:打开终端(ctrl + shift + T)
Run Code Online (Sandbox Code Playgroud)Go to the Extracted folder $ cd ~/Desktop/Python-3.7.0 $ ./configure $ make $ sudo make install将Python 3.7设为默认Python:
Run Code Online (Sandbox Code Playgroud)$ sudo vim ~/.bashrc press i on the last and new line - Type alias python= python3.7 press Esc type - to save and exit vim :wq now type $ source ~/.bashrc从这里:https : //www.quora.com/How-can-I-upgrade-Python-3-6-to-3-7-in-Ubuntu-18-04
我已经下载了几个模块,pip install module但是当我尝试导入它们时,我得到了一个 …
我正在尝试从此站点重现代码:https : //www.guru99.com/python-copy-file.html
一般的想法是使用python复制文件。虽然我可以解决错误,但我也想了解在这种情况下我做错了什么。
import shutil
from os import path
def main(filename):
if path.exists(filename):
src = path.realpath(filename)
head, tail = path.split(src)
dst = src + ".bak"
shutil.copy(src,dst)
main('C:\\Users\\test.txt') #This raises the error
main('test.txt') #This works, if the file is in the same folder as the py script
Run Code Online (Sandbox Code Playgroud)
如果与完整目录一起使用 (main('C:\Users\test.txt')) 代码返回错误AttributeError: 'str' object has no attribute 'exists'。如果我删除该行,path.exists()我会收到类似的错误:AttributeError: 'str' object has no attribute 'realpath'. 通过使用文件名,main('test.txt')一切正常,只要文件与包含该函数的 python 脚本位于同一文件夹中。 …
我在更广泛的范围内提出这个问题,因为我现在还没有面对这个特定的问题,但是我想知道将来该如何做。
如果我运行的python脚本运行时间长,那应该一直在做某事(如果有帮助,可以是infine循环)。通过python main.py在终端上运行命令来启动代码。
该代码没有结尾,因此没有sys.exit()。
我不想使用KeyboardInterrupt,也不想取消任务。因为这些选项是突然的,所以您无法准确预测停止代码的时间。
当我最终决定修改代码时,有没有办法“软”地终止代码?例如,使用另一个命令,准备一个类或运行另一个脚本?
最佳做法是什么?
PS .:请记住,我是新手程序员。
编辑: 我添加一些通用代码,以使我的问题更清楚。
import time,csv
import GenericAPI
class GenericDataCollector:
def __init__(self):
self.generic_api = GenericAPI()
def collect_data(self):
while True: #Maybe this could be a var that is changed from outside of the class?
data = self.generic_api.fetch_data() #Returns a JSON with some data
self.write_on_csv(data)
time.sleep(1)
def write_on_csv(self, data):
with open('file.csv','wt') as f:
writer = csv.writer(f)
writer.writerow(data)
def run():
obj = GenericDataCollector()
obj.collect_data()
if __name__ == "__main__":
run()
Run Code Online (Sandbox Code Playgroud)
在这种特殊情况下,该类从某个通用API(JSON中提供)收集数据,并将其无限循环地写入csv文件中。我如何编码一种方法(方法?)以在不突然中断(Ctrl + …
TL;DR: 我想创建 asyncio 任务/协程并获取要在 var 中分配的返回值。
——
我发现这个问题从作为 asyncio 任务运行的函数中获取值 这似乎在谈论一个类似的问题,但是 asyncio 模块中的 sintax 已经改变了很多,我什至不确定它是否相关。[我在 Python 3.7.2 上]
我的示例代码来解释我正在尝试做的事情:
async def s(f):
func = {
1: op1,
2: op2,
3: op3
}.get(f,False)
var = func(f)
return var
def op1(f):
print('Op1',f+f)
return f+f
def op2(f):
print('Op2', f*f)
return f*f
def op3(f):
print('Op3', f**f)
return f**f
async def main():
task1 = asyncio.create_task(s(1))
task2 = asyncio.create_task(s(3))
print(f"started at {time.strftime('%X')}")
await task1
await task2
print(task1,task2)
print(f"finished at {time.strftime('%X')}")
Run Code Online (Sandbox Code Playgroud)
main() …
我可以访问请求对象中的方法或属性,以完全按照客户端的请求返回 URL?包括查询参数?
我request.build_absolute_uri在查看这个问题后进行了检查,但它只返回没有查询参数的 URL。
我需要 URL,因为我的 API 响应返回结果“下一页”的 URL。我可以从query_params属性构建它,但是这个视图需要很多查询参数,有些排除了其他参数,所以访问请求 url 会为我省去很多麻烦。
我正在尝试使用python开关编写代码,如在dict中所建议的那样模拟:替换Python中的switch语句?[第二答案,尼克]
当我测试时,出于某种原因,内部的所有函数都被调用,尝试了一个简单的代码来查看出了什么问题并重复了问题。看到:
def switch(f):
print('Switch got: ', f)
var = {
1: func1(),
2: func2(),
3: func3()
}.get(f,False)
return var
def func3():
fb = 'Func3 called'
print(fb)
return fb
def func1():
rsp = 'Func1 called'
print(rsp)
return rsp
def func2():
rsp = 'Func2 called'
print(rsp)
return rsp
var = switch(1)
print(var)
Run Code Online (Sandbox Code Playgroud)
我的预期回报是:
Switch got: 1
Func1 called
Run Code Online (Sandbox Code Playgroud)
相反,我得到了:
Switch got: 1
Func1 called
Func2 called
Func3 called
Func1 called
Run Code Online (Sandbox Code Playgroud)
我从中得到的是,python似乎在调用适当的键(示例中为1)之前在dict中运行了所有func。
这是Python中的预期行为吗?
有没有办法解决,仅调用与键对应的功能?
我有一个调用 API 并生成数据的函数。后来我用来next()从生成器中检索数据,但是由于我不知道有多少数据要“提取”,我最终会执行next()直到它引发StopIteration异常。
def get_data():
source = API_Instance()
yield source.get_some_data()
def parse_data():
data = get_data()
while True:
try:
row_data = next(data)
print(row_data)
except StopIteration:
break
Run Code Online (Sandbox Code Playgroud)
这似乎是一种糟糕的方法。有没有办法避免 Try/Except 块?像知道发电机已用尽的方法吗?(找不到更好的词来形容它)