我正在尝试在Perl中生成一个与Python中生成的字符串等效的压缩字符串。
Python(产生正确的输出):
import gzip
test_file = 'example.json'
with open(test_file) as f:
output_data = gzip.compress(f.read().encode())
print(output_data)
或多或少我一直在Perl中尝试做的事情:
use Encode;
use IO::Compress::Gzip qw/gzip/;
use File::Slurp;
my $json = read_file('example.json', { binmode => ':raw' });
my $utf8_record = encode("utf8", $json);
my $output_data;
gzip \$utf8_record => \$output_data;
print $output_data;
您知道我在Perl方面做错了什么吗?
您对Perl没做错任何事情。您的Python代码和Perl代码都可以使用gzip正确压缩数据。只有Perl将输出打印为二进制,而您的Python代码则将输出打印为可读的表示形式,例如b"\x1f\x8b..."
。如果您使用print(...)