Python GZIP 圧縮における「TypeError: 'str' はバッファ インターフェイスをサポートしていません」のトラブルシューティング
Python の gzip モジュールを使用して文字列を圧縮する場合、開発者は、「TypeError: 'str' はバッファ インターフェイスをサポートしていません。」というエラーが発生する可能性があります。このエラーは、Python 2.x から Python 3.x への移行で文字列データ型が変更されたことが原因で発生します。
この問題を解決するには、文字列をバイトにエンコードしてから、文字列を書き込む必要があります。出力ファイル。次のコード スニペットは、質問で提供されたコードを修正します:
plaintext = input("Please enter the text you want to compress").encode('UTF-8') filename = input("Please enter the desired filename") with gzip.open(filename + ".gz", "wb") as outfile: outfile.write(plaintext)
プレーンテキストをバイトにエンコードすることで、Python 3.x の改訂された文字列データ型との互換性が確保されます。さらに、混乱を避けるために、「プレーンテキスト」と「ファイル名」は Python の予約語であるため、入力ファイルと出力ファイルに異なる変数名を使用することをお勧めします。
たとえば、次のコードは、次の方法を示しています。 UTF-8 エンコードを使用してポーランド語のテキストを効果的に圧縮します:
text = 'Polish text: ąćęłńóśźżĄĆĘŁŃÓŚŹŻ' filename = 'foo.gz' with gzip.open(filename, 'wb') as outfile: outfile.write(bytes(text, 'UTF-8')) with gzip.open(filename, 'r') as infile: decompressed_text = infile.read().decode('UTF-8') print(decompressed_text)
以上がPython GZIP 圧縮で「TypeError: \'str\' はバッファ インターフェイスをサポートしていません」を修正する方法?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond

pythonisnotpurelyLepted; itusesahybridapproachofbytecodecodecodecodecodecodedruntimerttation.1)pythoncompilessourcodeintobytecode、whodythepythonvirtualmachine(pvm).2)

ToconcatenateListsinpythothesheElements、使用:1)Operatortokeepduplicates、2)asettoremoveduplicates、or3)listcomplunting for controloverduplicates、各メトドハスディフェルフェルフェントパフォーマンスアンドソーダーインプリテーション。

pythonisantertedlanguage、useaseofuseandflexibility-butfactingporformantationationsincriticalapplications.1)解釈されたlikepythonexecuteline-by-lineを解釈します

Useforloopswhenthenumberofiterationsisknowninadvance、andwhiloopswheniterationsdependonacondition.1)forloopsareidealforsecenceslikelistoranges.2)


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

WebStorm Mac版
便利なJavaScript開発ツール

SublimeText3 中国語版
中国語版、とても使いやすい

mPDF
mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール
