我正在使用一个检索媒体的 API,但该 API 将媒体作为字节串返回...我想将字节串保存为图像。我该怎么做呢?
我试过这个:
data = *bytestring*
f = open('image.jpg','w')
f.write(str(data))
f.close()
Run Code Online (Sandbox Code Playgroud)
编译成功,但是当我检查 image.jpg 时...它是空的或“无法打开,因为它是未知格式”
我经常使用ghci它进行少量计算并stack ghci处理我的实际项目。
为了使第一个更容易,我编写了一个.ghci包含许多导入模块的文件,但其中一些模块不存在于我的堆栈项目中,并且我遇到了严重的错误。
目前我使用别名stackghci="stack ghci --ghci-options -ignore-dot-ghci",但随后我再次出现默认提示等等。
有没有办法指定两个.ghci文件;一个用于堆栈,一个用于ghci?
操作系统:openSUSE Tumbleweed 最新
正如堆栈文档所说,我可以使用 Tumbleweed 上默认存储库中的 zypper 安装堆栈。到 bin 的路径是/usr/bin/stack。
我遵循官方主页上的堆栈教程并执行stack new helloworld new-template(有效)。后cd进入文件夹,我想跑stack setup,但是这一次失败:
The GHC located at /usr/bin/ghc failed to compile a sanity check. Please see:
http://docs.haskellstack.org/en/stable/install_and_upgrade/
for more information. Exception was:
Running /usr/bin/ghc /tmp/stack-sanity-check9034/Main.hs
-no-user-package-db in directory /tmp/stack-sanity-check9034/
exited with ExitFailure 1
/tmp/stack-sanity-check9034/Main.hs:1:8:
Could not find module ‘Distribution.Simple’
Use -v to see a list of the files searched for.
Run Code Online (Sandbox Code Playgroud)
经过一番谷歌搜索后,我仍然不知道如何解决该错误。这就像我错过了一个非常基本的东西。
我有一个字符串,我使用str.encode('utf-8'). 之后我期望getsizeof()并len()返回相同的值,但似乎sys.getsizeof()总是返回更大的值。
然后我通过套接字将此二进制数据发送到 node.js 服务器并将它们存储在缓冲区中。双方Buffer.length并Buffer.byteLength返回相同的值,这等于len()在Python值。
我无法弄清楚那里发生了什么以及为什么Buffer.byteLength与sys.getsizeof().
我的数据并不总是字符串或可能有不同的编码,所以我想确保我知道以字节为单位的大小,而不是以字符为单位。
我正在尝试在Haskell项目中包含特定版本的库.该库是住宿和早餐(用于martix操作),但我需要特定版本0.4.3修复了乘法实现的错误.
所以,我的stack.yaml看起来像这样:
flags: {}
extra-package-dbs: []
packages:
- .
extra-deps:
- bed-and-breakfast-0.4
- base-4.6.0.1
resolver: lts-12.8
Run Code Online (Sandbox Code Playgroud)
但是我在构建时遇到了这个错误:
Error: While constructing the build plan, the following exceptions were encountered:
In the dependencies for bed-and-breakfast-0.4:
base-4.11.1.0 from stack configuration does not match >=4.5 && <4.7 (latest matching version
is 4.6.0.1)
needed due to realworldhaskell-0.1.0.0 -> bed-and-breakfast-0.4
Some different approaches to resolving this:
* Set 'allow-newer: true' to ignore all version constraints and build anyway.
* Consider trying 'stack solver', which uses the …Run Code Online (Sandbox Code Playgroud) 我正在尝试从电子邮件中读取附加的 .xlsx 文件。
我已经能够检索具有email.message.Messagetype 一部分的类型application/vnd.openxmlformats-officedocument.spreadsheetml.sheet。我应该能够阅读它使用
file = part.get_payload(decode=True)
Run Code Online (Sandbox Code Playgroud)
这给了我一个以字节开头的对象
b'PK\x03\x04\x14\x00\x06\x00\x08\x00\x00\x00!\x00\x93\xe11\xb6\x93\x01\x00\x003\x07\x00\x00\x13\x00\
Run Code Online (Sandbox Code Playgroud)
我想使用以下方法将其解析为字典
io.BytesIO(gzip.decompress(file))
Run Code Online (Sandbox Code Playgroud)
对于某些带有 .csv 压缩文件的电子邮件,这有效,但 .xlsx 文件无法使用此方法打开。我在网上查过,但我找不到任何解决方案。任何帮助将不胜感激。
在python2中,我可以整天生成以字符串格式表示的这些十六进制字节
'\x00\xaa\xff'
>>>’00'.decode('hex') + 'aa'.decode('hex') + 'ff'.decode('hex')
>>>'\x00\xaa\xff'
Run Code Online (Sandbox Code Playgroud)
同样,我可以在 python3 中做到这一点
>>> bytes.fromhex(’00’) + bytes.fromhex(‘aa’) + bytes.fromhex(‘ff’)
>>>b'\x00\xaa\xff'
Run Code Online (Sandbox Code Playgroud)
根据py2->py3这里的变化
Python 3.0 使用文本和(二进制)数据的概念,而不是 Unicode 字符串和 8 位字符串。所有文本都是Unicode;但是编码后的 Unicode 表示为二进制数据。
所以 Py2 版本的输出是一个字符串,而 Py3 版本的输出是字节类型的二进制数据
但我真的需要一个字符串版本!
根据上述文档:
由于 str 和 bytes 类型不能混合使用,您必须始终在它们之间进行显式转换。使用 str.encode() 从 str 转到 bytes,使用 bytes.decode() 从 bytes 转到 str。您还可以分别使用 bytes(s, encoding=...) 和 str(b, encoding=...) 。
好的,现在我必须解码这个字节类型的二进制数据......
>>> b'\x00\xaa\xff'.decode()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xaa in …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写具有多个输入的字符串,但该字符串应该转换为字节,因为我正在编写一个连接到服务器(使用套接字)的程序,该程序使用需要以字节接收消息的协议。我正在执行以下代码:
print("Please introduce your username: ")
username = input()
print("Please introduce your password: ")
password = input()
client_socket.send(b"AUTH:%s:%s\n"%(username, password))
Run Code Online (Sandbox Code Playgroud)
它给了我以下错误:
%b requires a bytes-like object, or an object that implements ____bytes____, not 'str'
Run Code Online (Sandbox Code Playgroud)
服务器应该收到以下消息:
AUTH:username:password
Run Code Online (Sandbox Code Playgroud)
使用适当的用户名和密码才能登录。
您对如何进行这项工作有任何想法吗?
我有一个haskell 赋值,其中我必须创建一个包含2 个参数的函数lastDigit xy 来计算所有[x^x | (0..x)],我的太慢了,我需要加快速度。任何人有任何想法?
list :: Integral x=>x->[x]
list 0 = []
list x = list(div x 10) ++ [(mod x 10)]
sqrall :: Integer->[Integer]
sqrall x y = [mod (mod x 10^y)^x 10^y | x <- [1..x]]
lastDigits :: Integer -> Int -> [Integer]
lastDigits x y = drop (length((list(sum (sqrall x y))))-y) (list(sum (sqrall x)))
Run Code Online (Sandbox Code Playgroud) 我有一些库,它具有使用 gnuplot 库进行绘图的功能:
import Graphics.Gnuplot.Simple
drawMap :: [(Int, Int)] -> IO ()
drawMap m = do
!a <- plotList [] m
print a
Run Code Online (Sandbox Code Playgroud)
我像这样从 main 调用这个函数:
main = do
!a <- drawMap [(1,2),(3,5)]
print a
Run Code Online (Sandbox Code Playgroud)
我用堆栈构建项目,并尝试了 -O2 和 -O0 优化,但 plot 永远print a不起作用(函数总是被成功调用并打印())。我怎样才能强制绘图以及为什么它不工作,使用库,但如果我只是plotList从 main调用就可以工作?
更新。
在 main 和drawMapby$!中使用严格的应用程序也不起作用:
drawMap :: [(Int, Int)] -> IO ()
drawMap m = plotList [] $! m
main = do
drawMap $! [(1,2),(3,5)]
Run Code Online (Sandbox Code Playgroud)
UPD 2 …