Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
golang和python中相同字符串的不同字节数组_Python_Python 3.x_Go_Compression_Gzip - Fatal编程技术网

golang和python中相同字符串的不同字节数组

golang和python中相同字符串的不同字节数组,python,python-3.x,go,compression,gzip,Python,Python 3.x,Go,Compression,Gzip,我正在使用python gzip模块压缩字符串,并在golang中解压 在python中压缩字符串的模块: gzip.compress(json.dumps("hello").encode(),-1) golang中用于对其进行解压缩的模块: func Decompression(compData []byte) ([]byte, error) { gr, err := gzip.NewReader(bytes.NewBuffer(compData)) if err != ni

我正在使用python gzip模块压缩字符串,并在golang中解压

在python中压缩字符串的模块:

gzip.compress(json.dumps("hello").encode(),-1)
golang中用于对其进行解压缩的模块:

func Decompression(compData []byte) ([]byte, error) {
    gr, err := gzip.NewReader(bytes.NewBuffer(compData))
    if err != nil {
        return compData, err
    }
    defer gr.Close()
    data, err := ioutil.ReadAll(gr)
    if err != nil {
        return nil, err
    }
    return data, err
}
获取错误:
Gzip:头无效

在压缩和解压缩时使用相同的压缩级别


有人能指出我犯的错误吗

我试图复制,使用此代码(python3)生成文件
out.bin

import gzip
import json

with open('out.bin', 'wb') as f:
    f.write(gzip.compress(json.dumps("hello").encode(), -1))

print(json.dumps("hello"))
此代码用于读取文件并对其进行解压缩(使用
解压缩功能:

package main

import (
    "bytes"
    "compress/gzip"
    "fmt"
    "io/ioutil"
)

func Decompression(compData []byte) ([]byte, error) {
    gr, err := gzip.NewReader(bytes.NewBuffer(compData))
    if err != nil {
        return compData, err
    }
    defer gr.Close()
    data, err := ioutil.ReadAll(gr)
    if err != nil {
        return nil, err
    }
    return data, err
}

func main() {
    d, err := ioutil.ReadFile("out.bin")
    if err != nil {
        panic(err)
    }
    fmt.Printf("raw data: %q\n", string(d))
    data, err := Decompression(d)
    if err != nil {
        panic(err)
    }
    fmt.Printf("decompressed: %q\n", string(data))
}

组合工作没有错误。可能您没有以某种方式从Python正确保存文件?您能在数据文件上运行我的go代码并显示原始字节吗?

由于您的问题中缺少详细信息,因此胡乱猜测:您使用了
print
以Python而不是
write
print
ma输出压缩结果从二进制值中提取一个可打印的字符串,即
b'\x1f\x8b\x08…
。这当然是无效的gzip。没有提到python语句中的打印。@Steffen我需要将压缩数据发布到kafka,请在该方向上提供帮助。“我需要将压缩数据发布到kafka,请在该方向上提供帮助。”-这是一个完全不同的问题,应该作为一个新问题来问。但也许可以看看压缩输入的预期效果。或者只是使用。