Python 2 で JSON を解析するときに文字列型を保持する
Python 2 で JSON を解析すると、文字列値が Unicode オブジェクトにキャストされる場合があります。これは、文字列オブジェクトのみを必要とするライブラリとインターフェースするときに問題が発生する可能性があります。
JSON から Unicode オブジェクトへの変換
JSON または simplejson を使用して ASCII エンコードされたテキスト ファイルをロードする場合Python 2 では、文字列値が Unicode オブジェクトに変換されるときに問題が発生する可能性があります。これは、テキストに Unicode を前提とする Python 2 のデフォルトのエンコーディング メカニズムによって説明されます。
代替解析オプション: PyYAML
この問題を解決するには、PyYAML を使用して JSON を解析することを検討してください。ファイル。 PyYAML はデフォルトでキーと値を文字列オブジェクトとして扱い、文字列型を保持するための簡単なソリューションを提供します。
例
<code class="python">import yaml list_dump = json.dumps(['a', 'b']) yaml.safe_load(list_dump) # Returns string objects: ['a', 'b']</code>
メモ:
- JSON 解析には PyYAML のsafe_load 関数が推奨されます。
- Unicode 文字は引き続き文字列オブジェクトに保持されます。
- Ruamel YAML は、より多くの機能を備えた代替 YAML パーサーです。高度な機能。
変換関数
PyYAML がオプションではない場合、または Unicode を使用する必要がある場合は、Mark によって提案されたような変換関数Amery を利用することができます。
例
<code class="python">def convert_to_str(data): """Convert Unicode values to strings.""" if isinstance(data, dict): return {convert_to_str(k): convert_to_str(v) for k, v in data.items()} elif isinstance(data, list): return [convert_to_str(v) for v in data] elif isinstance(data, unicode): return data.encode('utf-8') else: return data</code>
JSON ロード時にこの関数を object_hook として使用すると、Unicode 値が文字列に変換されます。
以上がPython 2 で JSON を解析するときに文字列型を保持するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

forhandlinglaredataSetsinpython、usenumpyArrays forbetterperformance.1)numpyarraysarememory-effictientandfasterfornumericaloperations.2)nusinnnnedarytypeconversions.3)レバレッジベクトル化は、測定済みのマネージメーシェイメージーウェイズデイタイです

inpython、listsusedynamicmemoryallocation with allocation、whilenumpyArraysalocatefixedmemory.1)listsallocatemorememorythanneededededinitivative.2)numpyArrayasallocateexactmemoryforements、rededicablebutlessflexibilityを提供します。

inpython、youcanspecthedatatypeyfelemeremodelernspant.1)usenpynernrump.1)usenpynerp.dloatp.ploatm64、フォーマーpreciscontrolatatypes。

numpyisessentialfornumericalcomputinginpythonduetoitsspeed、memory efficiency、andcomprehensivematicalfunctions.1)それは、performsoperations.2)numpyArraysaremoremory-efficientthanpythonlists.3)Itofderangeofmathematicaloperty

contiguousMemoryAllocationisucial forArraysは、ForeffienceAndfastelementAccess.1)iteenablesConstantTimeAccess、O(1)、DuetodirectAddresscalculation.2)itemprovesefficiencyByAllowingMultiblementFechesperCacheLine.3)itimplifieMememm

slicingapythonlistisdoneusingtheyntaxlist [start:stop:step] .hore'showitworks:1)startisthe indexofthefirstelementtoinclude.2)spotisthe indexofthefirmenttoeexclude.3)staptistheincrementbetbetinelements

numpyallows forvariousoperationsonarrays:1)basicarithmeticlikeaddition、減算、乗算、および分割; 2)AdvancedperationssuchasmatrixMultiplication;

Arraysinpython、特にnumpyandpandas、aresentialfordataanalysis、offeringspeedandeficiency.1)numpyarraysenable numpyarraysenable handling forlaredatasents andcomplexoperationslikemoverages.2)Pandasextendsnumpy'scapabivitieswithdataframesfortruc


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

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

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

ホットトピック









