我有两次,一个开始和一个停止时间,格式为10:33:26(HH:MM:SS).我需要两次之间的区别.我一直在浏览Python的文档和在线搜索,我想它会与日期时间和/或时间模块有关.我不能让它正常工作,并在涉及日期时继续只查找如何执行此操作.
最终,我需要计算多个持续时间的平均值.我得到了时间差异,我将它们存储在列表中.我现在需要计算平均值.我正在使用正则表达式解析原始时间然后做差异.
对于平均,我应该转换为秒然后平均?
我想将matplotlib图表直接嵌入到由ReportLab生成的PDF中 - 即不首先保存为PNG,然后将PNG嵌入到PDF中(我想我会得到更好的输出质量).
有谁知道是否有一个matplotlib可流动的ReportLab?
谢谢
我正在尝试编写一个给出非负整数列表的函数,将它们排列成最大可能的数字.
例如,给定[50, 2, 1, 9],最大的数字是95021.
这是我试图解决问题的代码:
a = [50, 2, 1, 9]
a.sort()
ans = []
for i in range(len(a)-1,-1,-1):
ans.append(a[i])
print ''.join(map(str,ans))
Run Code Online (Sandbox Code Playgroud)
不过,我得到50921,因为50是最大的,但它应该显示9第一.
查看以前的答案但无法解决它,json.loads 不起作用。
编码:
import json
import operator
Data_to_python ={}
Bank_Data_note= open('Data_for_python.txt','r') # open file
Bank_Data_str = Bank_Data_note.read()
print(Bank_Data_str)
Data_to_python =json.loads(Bank_Data_str) # dictinary
print(Data_to_python)
Run Code Online (Sandbox Code Playgroud)
来自文本文件的 json 格式:
{{"Transaction_1":{"Name":"Magnolia","Location":"Ayilon male","Amount":289,"Date":"5/5/18"},
{"Transaction_2":{"Name":"Landver,"Location":"Cinima-city Ramat-hashron","Amount":15,"Date":"15/5/18"},
{"Transaction_3":{"Name":"Superfarm","Location":"Shivat-hacochvim male","Amount":199,"Date":"7/5/18"},
{"Transaction_4":{"Name":"Printing solutions","Location":"Afeka tel-aviv","Amount":16,"Date":"25/5/18"}}
Run Code Online (Sandbox Code Playgroud)
我懂了:
obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError:期望用双引号括起来的属性名称:第 1 行第 2 列(字符 1)
'u'在打印列表中的元素之前?我没有在我的代码中输入你.
hobbies = []
#prompt user three times for hobbies
for i in range(3):
hobby = raw_input('Enter a hobby:')
hobbies.append(hobby)
#print list stored in hobbies
print hobbies
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它打印列表但它的格式如下:
Enter a hobby: Painting
Enter a hobby: Stargazing
Enter a hobby: Reading
[u'Painting', u'Stargazing', u'Reading']
None
Run Code Online (Sandbox Code Playgroud)
那些'你'在列表的每个元素之前来自哪里?
我想把这个字符串:
a = '\\a'
Run Code Online (Sandbox Code Playgroud)
进入这个
b = '\a'
Run Code Online (Sandbox Code Playgroud)
似乎有一种明显的方法可以做到这一点replace?
编辑:更确切地说,我想改变反斜杠的转义以逃避角色 a
我正在使用popen制作一个插件,这个插件使用了一个外部程序,在输出中显示了一些彩色文本.
输出是这样的:
avr-g++ -o .pioenvs\uno\FrameworkArduino\HardwareSerial.o -c -std=gnu++11 -fno-exceptions -fno-threadsafe-statics -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO_AVR_UNO -DARDUINO=10607 -I.pioenvs\uno\FrameworkArduino -I.pioenvs\uno\FrameworkArduinoVariant .pioenvs\uno\FrameworkArduino\HardwareSerial.cpp
avr-g++ -o .pioenvs\uno\FrameworkArduino\HardwareSerial0.o -c -std=gnu++11 -fno-exceptions -fno-threadsafe-statics -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO_AVR_UNO -DARDUINO=10607 -I.pioenvs\uno\FrameworkArduino -I.pioenvs\uno\FrameworkArduinoVariant .pioenvs\uno\FrameworkArduino\HardwareSerial0.cpp
=============================================
Path\file.cpp: in function 'void loop()':
Path\file.cpp:23:2 error: expected ';' before '}' token
}
^
=============================================
Run Code Online (Sandbox Code Playgroud)
所有内部"="都是红色和黄色.
当我在命令控制台中运行命令时,我可以看到完整的输出,但是当我使用Popen时,我只能获得未着色的文本
这就是我使用Popen的方式
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=self.cwd, universal_newlines=True, shell=True)
output = process.communicate()
stdout = output[0]
stderr = output[1]
print(stdout)
print(stderr) …Run Code Online (Sandbox Code Playgroud) 我试图从别人的项目中理解这段代码.如果您想要上下文,请访问:https://github.com/newsapps/beeswithmachineguns/blob/master/beeswithmachineguns/bees.py#L501
IS_PY2只是一个布尔变量,True如果Python主要版本是2.我知道非空字符串是True,但由于某种原因我不理解openmode被指定'w'或者'wt'而不是True或False.
openmode = IS_PY2 and 'w' or 'wt'
openkwargs = IS_PY2 and {} or {'encoding': 'utf-8', 'newline': ''}
Run Code Online (Sandbox Code Playgroud)
有人能解释一下结果吗?
我已经读过Raymond Hettinger实现紧凑型dicts 的新方法.这解释了为什么Python 3.6中的dicts使用的内存少于Python 2.7-3.5中的dicts.但是,Python 2.7和3.3-3.5 dicts中使用的内存之间似乎存在差异.测试代码:
import sys
d = {i: i for i in range(n)}
print(sys.getsizeof(d))
Run Code Online (Sandbox Code Playgroud)
如上所述,我理解3.5和3.6之间的节省,但我很好奇节省2.7和3.5之间的原因.
我想要最像Pythonic的方式来舍入数字就像Javascript一样(通过Math.round()).它们实际上略有不同,但这种差异可能会对我的应用产生巨大影响.
使用round()Python 3中的方法:
// Returns the value 20
x = round(20.49)
// Returns the value 20
x = round(20.5)
// Returns the value -20
x = round(-20.5)
// Returns the value -21
x = round(-20.51)
Run Code Online (Sandbox Code Playgroud)
使用Math.round()Javascript*中的方法:
// Returns the value 20
x = Math.round(20.49);
// Returns the value 21
x = Math.round(20.5);
// Returns the value -20
x = Math.round(-20.5);
// Returns the value -21
x = Math.round(-20.51);
Run Code Online (Sandbox Code Playgroud)
谢谢!
参考文献:
python ×10
python-2.7 ×4
python-2.x ×2
python-3.x ×2
dictionary ×1
javascript ×1
json ×1
math ×1
matplotlib ×1
output ×1
popen ×1
printing ×1
python-2.6 ×1
reportlab ×1
time ×1
unicode ×1