我想要最像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)
谢谢!
参考文献:
我刮这个链接与BeautifulSoup4
我像这样解析页面HTML
page = BeautifulSoup(page.replace('ISO-8859-1', 'utf-8'),"html5lib")
Run Code Online (Sandbox Code Playgroud)
你可以看到像这样的值-4 -115(分隔-)
我希望列表中的两个值,所以我使用这个正则表达式.
value = re.findall(r'[+-]?\d+', value)
Run Code Online (Sandbox Code Playgroud)
它完美无缺,但不是这些值+2½ -102,我只能得到[-102]
为了解决这个问题,我也试过了
value = value.replace("½","0.5")
value = re.findall(r'[+-]?\d+', value)
Run Code Online (Sandbox Code Playgroud)
但这给了我关于编码的错误,说我必须设置我的文件的编码.
我也试过设置encoding=utf-8在文件顶部,但仍然给出相同的错误.
我需要问我如何转换½为0.5
我已经从Web爬网脚本中提取了一个字符串,如下所示:
u'\xe3\x80\x90\xe4\xb8\xad\xe5\xad\x97\xe3\x80\x91'
Run Code Online (Sandbox Code Playgroud)
我想u'\xe3\x80\x90\xe4\xb8\xad\xe5\xad\x97\xe3\x80\x91'用utf-8 解码。使用http://ddecode.com/hexdecoder/,我可以看到结果是'????'
我尝试使用以下语法,但失败了。
msg = u'\xe3\x80\x90\xe4\xb8\xad\xe5\xad\x97\xe3\x80\x91'
result = msg.decode('utf8')
Run Code Online (Sandbox Code Playgroud)
错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-11: ordi
nal not in range(128)
Run Code Online (Sandbox Code Playgroud)
请问如何正确解码字符串?
感谢帮助。
我正在尝试从 python 脚本中读取一个日志文件。我的程序在 Linux 中运行良好,但在 windows 中出现错误。在特定行号读取某些行后,出现以下错误
File "C:\Python\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 311: char
acter maps to <undefined>
Run Code Online (Sandbox Code Playgroud)
以下是我用来读取文件的代码
with open(log_file, 'r') as log_file_fh:
for line in log_file_fh:
print(line)
Run Code Online (Sandbox Code Playgroud)
我试图通过使用不同的编码模式来修复它,如 ascii、utf8、utf-8、ISO-8859-1、cp1252、cp850。但仍然面临同样的问题。有什么办法可以解决这个问题。
加载名称接受其参数,并将存储名称存储的名称值推入参数指示的位置。load global的功能类似,但是字节码中似乎没有全局存储。那么有什么区别以及如何加载全局工作
我想在Python 3.3中使用Windows中的路径,但是我有一个错误:
FileNotFoundError:[Errno 2]没有这样的文件或目录:'E:\\ dir \\ .project'
问题是双反斜杠.我用r读了解决方案.
def f(dir_from):
list_of_directory = os.listdir(dir_from)
for element in list_of_directory:
if os.path.isfile(os.path.join(dir_from, element)):
open(os.path.join(dir_from, element))
f(r'E:\\dir')
Run Code Online (Sandbox Code Playgroud)
我又有这个错误
FileNotFoundError:[Errno 2]没有这样的文件或目录:'E:\\ dir \\ .project'
os.path.normpath(path) 不解决我的问题.
我究竟做错了什么?
在一种有限的方式中,我想知道为什么在Python中使用裸露的东西是如此不受欢迎.
如果我有一个完整的程序运行,我会这样做:
import sys
from application import program
try:
program.start()
except:
print >> sys.stderr, "It didn't work"
Run Code Online (Sandbox Code Playgroud)
我正在压制重要信息并且没有真正的输出.但是让我们考虑我正在为配置设置进行IO操作,如果配置不存在或已损坏,我有一组模板要使用:
import os
from config import load_data
from template import save_data
dir = os.path.expanduser('~')
path = os.path.join(dir, 'config.txt')
try:
load_data(path)
except:
save_data(path)
Run Code Online (Sandbox Code Playgroud)
我想在这种情况下,构建一个捕获操作的自定义异常是否更好,以便为阅读代码的人提供更多信息?
对于较小的数字,它们似乎是相等的,但对于较大的数字,它们似乎不同。
例如:
a = int(1267650600228229401496703205376/10)
b = 1267650600228229401496703205376 // 10
print(a - b) # prints 7036874417767
a = int(1493845793475/10)
b = 1493845793475 // 10
print(a - b) # prints 0
Run Code Online (Sandbox Code Playgroud)
怎么会?
首先我知道已经有很多关于这个特殊错误的问题,但我找不到任何解决它的确切背景的问题.我也尝试过为其他类似错误提供的解决方案,但没有任何区别.
我正在使用python模块pickle将对象保存到文件并使用以下代码重新加载它:
with open('test_file.pkl', 'wb') as a:
pickle.dump(object1, a, pickle.HIGHEST_PROTOCOL)
Run Code Online (Sandbox Code Playgroud)
这不会抛出任何错误,但是当我尝试使用以下代码打开文件时:
with open('test_file.pkl', 'rb') as a:
object2 = pickle.load(a)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
---------------------------------------------------------------------------
RecursionError Traceback (most recent call last)
<ipython-input-3-8c5a70d147f7> in <module>()
1 with open('2test_bolfi_results.pkl', 'rb') as a:
----> 2 results = pickle.load(a)
3
~/.local/lib/python3.5/site-packages/elfi/methods/results.py in __getattr__(self, item)
95 def __getattr__(self, item):
96 """Allow more convenient access to items under self.meta."""
---> 97 if item in self.meta.keys():
98 return self.meta[item]
99 else:
... last 1 frames repeated, from the frame …Run Code Online (Sandbox Code Playgroud) 我需要在linux上的python进程之间有效地传输二进制数据.但是我找不到用管道传递它的方法.我得到的最接近的是将其作为字符串传递,但由于某种原因它保留了二进制形式,因此它给我带来了问题.
ciphertext, tag = cipher1.encrypt_and_digest(input().encode())
print(ciphertext)
Run Code Online (Sandbox Code Playgroud)
密文是二进制的,但是当我在另一个进程中测试它的类型时
print(type(sys.stdin.read()))
Run Code Online (Sandbox Code Playgroud)
我明白了
<class 'str'>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
python ×10
python-3.x ×5
python-2.7 ×2
backslash ×1
bytecode ×1
file ×1
javascript ×1
math ×1
pickle ×1
pipe ×1
python-2.x ×1
unicode ×1
utf-8 ×1