我不仅是python的新手,而是完全编程所以我非常感谢你的帮助!
我试图使用Tweepy过滤来自twitter流API的所有推文.
我已按用户ID进行过滤,并确认正在实时收集推文.
然而,似乎只有第二张最后一条推文是实时收集而不是最新的推文.
你们能帮忙吗?
import tweepy
import webbrowser
import time
import sys
consumer_key = 'xyz'
consumer_secret = 'zyx'
## Getting access key and secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth_url = auth.get_authorization_url()
print 'From your browser, please click AUTHORIZE APP and then copy the unique PIN: '
webbrowser.open(auth_url)
verifier = raw_input('PIN: ').strip()
auth.get_access_token(verifier)
access_key = auth.access_token.key
access_secret = auth.access_token.secret
## Authorizing account privileges
auth.set_access_token(access_key, access_secret)
## Get the local time
localtime = time.asctime( time.localtime(time.time()) )
## Status …Run Code Online (Sandbox Code Playgroud) 我正在使用Poolundermultiprocessing做一些事情。
def my_func(...):
#Different processes can take different time
print a, b, c #Value that I calculated above. (includes 2 new-lines)
Run Code Online (Sandbox Code Playgroud)
在某些情况下,一个进程的 a、b、c 的值没有一起打印。有没有办法避免这种情况?谁能解释一下这里发生了什么以及如何避免?
我的理解是,如果我删除打印中的所有换行符并在最后只保留一个换行符,它应该可以解决问题。(问题是,它不是每次都可以重现,所以我仍在测试东西)。
有没有一种方法可以sys.stdout专门用于一个过程,然后在我打印内容时将其释放STDOUT?
我正在运行一些带有sys.exit()调用的代码.如果没有sys.exit()行,self.response就可以正常工作并呈现模板.但是当我调用sys.exit()时,页面返回空白.这几乎就像sys.exit()正在中间渲染模板.它为什么这样做?
page = 'index.html'
template_values = {}
path = os.path.join(os.path.dirname(__file__), page)
self.response.out.write(template.render(path, template_values))
sys.exit()
Run Code Online (Sandbox Code Playgroud)
编辑我用"return"而不是"sys.exit()"解决了我的问题
我正在尝试做的是每次打印时添加0.5秒的延迟.但我遇到的问题是它滞后了一段时间然后立刻打印出来.
import time
x = [(0, 1), (2, 3), (4, 5), (6, 7), (8, 7)]
for i in x:
print(i)
time.sleep(0.5)
Run Code Online (Sandbox Code Playgroud)
这应该做什么:
(0, 1)
wait 0.5 seconds
(2, 3)
wait 0.5 seconds
(4, 5)
etc
Run Code Online (Sandbox Code Playgroud)
然而,我的问题是它没有打印第一个并等待0.5并打印下一个,我的工作是等待这么久然后立即打印所有,我想知道什么是解决这个问题的方法.
我有一个问题,将子进程的stdout转发到当前进程的stdout.
这是我的MWE调用者代码(runner.py):
import sys
import subprocess
import time
p = subprocess.Popen([sys.executable, "test.py"], stdout=sys.stdout)
time.sleep(10)
p.terminate()
Run Code Online (Sandbox Code Playgroud)
这是被调用者test.py的内容:
import time
while True:
time.sleep(1)
print "Heartbeat"
Run Code Online (Sandbox Code Playgroud)
以下将工作并将所有心跳打印到控制台:
python runner.py
Run Code Online (Sandbox Code Playgroud)
但是,以下不起作用,输出文本文件保持为空(使用Python 2.7):
python runner.py > test.txt
Run Code Online (Sandbox Code Playgroud)
我需要做什么?
我需要使用 Python 执行一些长时间的过程,该过程将使用 PHP(我的主要语言)调用并实时显示结果。
假设这是我的 Python 脚本(a.py)。
import time
for x in range(10):
print "S;adasdasd;",x
time.sleep(0.5)
Run Code Online (Sandbox Code Playgroud)
我尝试了很多来自互联网的例子,但总是得到相同的结果。PHP 总是等到脚本完成然后显示它。
这是我尝试过的众多代码之一。
header( 'Content-type: text/html; charset=utf-8' );
$handle = popen('python folder\\a.py', 'r');
while (!feof($handle)) {
echo fgets($handle);
flush();
ob_flush();
}
pclose($handle);
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
我有以下小python脚本来运行本地服务器来测试一些html:
print('opened')
from http.server import HTTPServer, SimpleHTTPRequestHandler
server_address = ('', 8000)
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
print("Listening at https://127.0.0.1:8000/ . . .")
httpd.serve_forever()
Run Code Online (Sandbox Code Playgroud)
当我在终端中运行它时,它会阻止print语句:没有打印.但服务器工作,我可以localhost:8000在浏览器中访问我的html文件.但是,如果我注释掉最后一行,则调用serve_forever(),它可以打印"打开"和"收听https:127.0.0.1:8000 /"..'.当然,它实际上并不起作用,因为现在服务器没有运行.
我觉得这很混乱.前一行在最后一行之前执行.为什么最后一行会导致前一行不起作用?
Windows7上的Python3,如果有人要问,但我怀疑这是相关的.
我需要在 Python 2.7 中在没有换行符的情况下将文本打印到控制台,因此我可以在以后的代码中继续在同一行上编写更多文本。我当前的实现涉及从未来库中导入 Python 3 打印函数,并使用 end=''。
这并不理想,就像我打印一行一样,例如:
print("We're doing something...",end='')
Run Code Online (Sandbox Code Playgroud)
然后使用任何其他代码,然后是一行,例如:
print("we finished doing that thing.")
Run Code Online (Sandbox Code Playgroud)
该行被打印,但它是一次打印的,这意味着它被缓冲,直到它得到包含换行符的打印。我更希望能够将第一个打印字符串输出到控制台,执行其他代码,然后放入带有换行符的部分。我无论如何都找不到在 Python 2.7 中使用打印来执行此操作的方法。也许有人可以给我指出一种功能性的方法来做到这一点?谢谢。
对于那些建议环境缓冲修复它的人来说,它没有。它会影响文件输出和其他一些与它无关的杂项。下面有一个总体上是实用的答案。
它在 VS Code 中运行良好,但在 powershell 中它会提示用户输入,但不会在“stdout”中显示字符串。这是示例代码:
import sys
def get_int():
sys.stdout.write("Enter number(s). ")
return map(int, sys.stdin.readline().strip().split())
def get_float():
sys.stdout.write("Enter number(s). ")
return map(float, sys.stdin.readline().strip().split())
def get_list_of_int():
sys.stdout.write("Enter numbers followed by space. ")
return list(map(int, sys.stdin.readline().strip().split()))
def get_string():
sys.stdout.write("Enter string. ")
return sys.stdin.readline().strip()
a, b, c, d = get_int()
e, f, g = get_float()
arr = get_list_of_int()
str = get_string()
Run Code Online (Sandbox Code Playgroud) 我试图使用随机打印随机数,但是当我尝试使用输出在一行中打印end= " "输出时不显示任何内容,直到我打破程序.
import random
import time
while True:
x = random.randint(1,6)
print(x, end=" ")
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
我打断后输出是这样的:
C1 2 3 5 5 4 5 4 1 ---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
Run Code Online (Sandbox Code Playgroud) python ×10
output ×2
php ×1
powershell ×1
python-2.7 ×1
python-3.x ×1
real-time ×1
shell ×1
stdout ×1
streaming ×1
string ×1
subprocess ×1
tweepy ×1
twitter ×1