我with今天第一次遇到了Python 语句.我已经使用Python几个月了,甚至不知道它的存在!鉴于其地位有点模糊,我认为值得问:
with设计用于的Python 语句? try..finally比它更好用的情况with?我一直对使用print语句输出到终端需要多长时间感到惊讶/沮丧.在最近的一些令人痛苦的缓慢记录之后,我决定调查它并且非常惊讶地发现几乎所有花费的时间都在等待终端处理结果.
能以某种方式加速写入stdout吗?
我写了一个脚本(print_timer.py在这个问题的底部),比较写入100k行到stdout,文件和stdout重定向到的时间/dev/null.这是时间结果:
$ python print_timer.py
this is a test
this is a test
<snipped 99997 lines>
this is a test
-----
timing summary (100k lines each)
-----
print :11.950 s
write to file (+ fsync) : 0.122 s
print with stdout = /dev/null : 0.050 s
Run Code Online (Sandbox Code Playgroud)
哇.为了确保python不在幕后做某事,比如认识到我将stdout重新分配给/ dev/null或其他东西,我在脚本之外进行了重定向...
$ python print_timer.py > /dev/null
-----
timing summary (100k lines each)
-----
print : 0.053 s
write to file (+fsync) : 0.108 s
print with …Run Code Online (Sandbox Code Playgroud) 所以我正在学习Python.我正在经历课程并遇到一个问题,我必须将一个很多人压缩target.write()成一个单独的write(),同时"\n"在每个用户输入变量(对象write())之间.
我提出了:
nl = "\n"
lines = line1, nl, line2, nl, line3, nl
textdoc.writelines(lines)
Run Code Online (Sandbox Code Playgroud)
如果我尝试做:
textdoc.write(lines)
Run Code Online (Sandbox Code Playgroud)
我收到一个错误.但如果我输入:
textdoc.write(line1 + "\n" + line2 + ....)
Run Code Online (Sandbox Code Playgroud)
然后它工作正常.为什么我无法使用字符串换行,write()但我可以使用它writelines()?
Python 2.7当我搜索google时,我发现的大多数资源都是我的头脑,我仍然是一个非常善良的人.
我试图将Python脚本的结果输出到文本文件,其中每个输出应保存到一行.
f1=open('./output.txt', 'a')
f1.write(content + "\n")
Run Code Online (Sandbox Code Playgroud)
当我output.txt用常规打开时,notepad结果如下所示:
color amber color aqua color analysis color app color adobe color alive app
Run Code Online (Sandbox Code Playgroud)
但是,当我打开文件notepad++时看起来很好,每个单词都保存在一行上.
如何使脚本逐行保存结果,以便在常规记事本中显示相同内容?
我正在使用cron启动一个python脚本,如下所示:
#Python script called every 3 minutes in case script crashes
*/3 * * * * /home/ubuntu/python_script.sh &
Run Code Online (Sandbox Code Playgroud)
我的脚本有几个打印用于调试.我想将这些消息打印到文件中,例如/home/ubuntu/Logs.txt
我的脚本还检查python调用是否已经在运行,因此它不会再次启动它:
#!/bin/bash
lines=$(ps -ef | grep python_script.py | wc -l)
if [ $lines -ne 2 ]; then
python python_script.py &
fi
Run Code Online (Sandbox Code Playgroud)
如何将消息打印到此文件?我正在使用python 2.7
更新:使用以下代码编辑我的代码:
1)将其添加为我的python脚本的第一行:
from __future__ import print_function
Run Code Online (Sandbox Code Playgroud)
2)在上次导入后打开日志文件,然后在其后添加测试打印:
log = open("/home/ubuntu/Logs.txt", "w+")
print("Testing...", file = log)
Run Code Online (Sandbox Code Playgroud)
如果我简单地从终端运行它,它打印到文件很好.但是如果我用cron安排它,它就不会写入文件.
更新2:使用"logger"命令结束了解决方案的绊脚石.在我的shell脚本中,我必须添加"2>&1"(在shell脚本中,而不是ElmoVanKielmo描述的crontab)和"| logger&".最终解决方案如下所示:
#!/bin/bash
lines=$(ps -ef | grep python_script.py | wc -l)
if [ $lines -ne 2 ]; then
python python_script.py 2>&1 | …Run Code Online (Sandbox Code Playgroud) 我想用 Python 2.7 编写一个 6 行的简单文本文件。我正在使用此代码:
import csv
export=open('./images2/test.tfw', "wb")
writer=csv.writer(export, delimiter=' ', quoting=csv.QUOTE_NONE)
writer.writerow('0.06')
writer.writerow('0.00')
writer.writerow('0.00')
writer.writerow('-0.06')
writer.writerow('-10.59')
writer.writerow('38.49')
export.close()
Run Code Online (Sandbox Code Playgroud)
我正在进入文件:
0 . 0 6
0 . 0 0
0 . 0 0
- 0 . 0 6
- 1 0 . 5 9
3 8 . 4 9
Run Code Online (Sandbox Code Playgroud)
但我不希望数字内有空格或其他任何东西,我只需要这样:
0.06
0.00
0.00
-0.06
-10.59
38.49
Run Code Online (Sandbox Code Playgroud)
但是当我尝试delimiter=''or 时delimiter=None,出现错误“必须设置分隔符”。如何在没有分隔符的情况下写我的数字?也许这是一个非常基本的问题,但我在谷歌中找不到。谢谢!
我正在尝试向文件中写入一些文本,这是我尝试过的:
text ="Lorem Ipsum is simply dummy text of the printing and typesetting " \
"industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s," \
" when an unknown printer took a galley of type and scrambled it to make a type specimen book."
target = open("file", 'wb')
target.writelines(text)
Run Code Online (Sandbox Code Playgroud)
而且我得到一个空文件。我怎样才能做到这一点?
我正在尝试创建一个程序,用户在其中输入字符串列表,每个字符串都在单独的行中。例如,我希望能够返回第二行中的第三个单词。下面的输入将返回“蓝色”。
input_string("""The cat in the hat
Red fish blue fish """)
Run Code Online (Sandbox Code Playgroud)
目前我有这个:
def input_string(input):
words = input.split('\n')
Run Code Online (Sandbox Code Playgroud)
所以我可以使用words[n]输出某一行,但是如何输出特定行中的特定单词呢?我一直在尝试实现能够输入单词[1][2],但我创建多维数组的尝试失败了。
我已经尝试拆分每个单词[n]几个小时,但谷歌没有帮助。如果这是完全显而易见的,我深表歉意,但我几天前才开始使用 Python,并且完全陷入困境。
我正在尝试使用 API(尤其是公司 API)并设法使其工作到可以使用命令行向 API 网站发出请求并返回数据的程度,我使用以下代码执行此操作:
r = requests.get('https://api.companieshouse.gov.uk/company/**COMPANY NUMBER**/filing-history', auth=('**AUTH CODE**', ''))
r.json()
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为它会在命令行界面中输出相关信息,但我真正想做的是将这些数据保存到我的计算机上,最好以 .json 格式保存。我在网上找到的大多数解决方案都涉及使用请求模块从互联网下载直接图像或网页,但在将文本转换为文件然后下载时无济于事。
希望这里有人能指出我正确的方向并帮助我,或者更糟糕的是,告诉我这是不可能的。如果您需要任何说明,请告诉我!
谢谢!!
我有一个格式的文本文件:
2
1 3
2 4
Run Code Online (Sandbox Code Playgroud)
我编写了一个 python 程序,它要求输入的数量并打印出值 sum 。
n = int(raw_input())
for _ in range(n):
a,b = map(int,raw_input().split())
print a+b
Run Code Online (Sandbox Code Playgroud)
但