データエンコーディングルールとファイルの読み取りと書き込み



Data Encoding Rules



コーディング、

これは、ある形式から別の形式に変換するプロセスです。デコードはエンコードの逆のプロセスです
あらかじめ決められた方法で、数字、テキスト、その他の情報をコードに変換したり、情報やデータを指定された電気パルス信号に変換したりします。
バイナリ:0または1を使用し、各数値はビットと呼ばれます
2進数の右側の最初の数値から始めて、それぞれに2をnの累乗で乗算し、nは0から始まり、1ずつ増加します。次に、各数値を合計して10進数を取得します。
例10010
1X2 4 + 0x2 3 + 0x2 2 + 1x2 1 + 0x2 ** 0 = 18
8ビットは1バイト(バイト)に等しい
bps =ビット/秒

  1. ACSLLエンコーディング:7ビットまたは8ビットのセカンダリエンコーディングを使用して、128または256で可能な文字を表します
    7ビットのバイナリ(残りの1ビットは0)を使用して、すべての大文字と小文字、数字、句読点を表します。

2. GB(National Standard)エンコーディング:GB2312とGBKは、より一般的に使用される2つです。 GBKエンコーディングは、GBコードの拡張であり、最大20,000の簡体字および繁体字中国語の文字をエンコードします。



  1. unicode:世界中のすべてのテキストを組み込み、Webページは複数の言語を表示することもできます。各文字は2バイトを占め、無駄です:UTF-8(8ビット
    Unicode変換形式)、これはUnicodeの可変長文字エンコードであり、1〜4バイトを使用してシンボルを表すことができ、バイト長はシンボルごとに異なります。また、UTF-8はASCIIエンコーディングとも互換性があります。
  • Unicodeはメモリエンコーディングの仕様であり、UTF-8はUnicodeを保存および送信する方法です。
  • 2、8、16はすべて2の累乗であり、相互変換に便利です。
  • 8進数システムは0、1、2、3、4、5、6、7を使用します。16進数システムは0、1、2、3、4、5、6、7、8、9、a、b、c、dを使用します。 、e、f。
  • Python入力デフォルトUnicode
    |エンコード|デコード|
    | Unicodeを他のプログラミング言語に変換することを意味します|例:name.encode( ‘GB2312’)|
    |デコード|他のプログラミング言語をUnicodeに変換することを意味します|

エンコード

print('Sisi'.encode('gbk')) print('Sisi'.encode('utf-8')) or str='xixi' str.encode()

デコード



bytes='xixi'.encode() bytes.decode() The default here is utf-8Decode, use others should be written as decode ('gbk') Mode

ファイルの読み取りと書き込み

ファイルを開く-ファイルを読む-ファイルを閉じる

file = open('File path.txt','r',encoding='utf-8') #'r' means read, which means to open the file in read mode file_data=file.read() print(file_data) file.close() #Close file

ファイル絶対パスWindowsシステム書き込み

# 1 Add escape character 'C:\Users\Administrator\Desktop\py_demo\class_16\verse.txt' Writing2 Add r before r'C:UsersAdministratorDesktoppy_democlass_16verse.txt'

書く

ファイルを開く-ファイルを書き込む-ファイルを閉じる



file=open('File path.txt','w',encoding=('utf-8')) #w means write file If you do not want to overwrite the original file, change w to a (append)

ファイルを書き込む別の方法があります:writelines()関数。文字列のリストなど、一連の文字列をファイルに書き込むために使用されます。ファイルに書き込まれるコードを次のように変更しますwritelines() Is:

file = open('verse.txt','a',encoding='utf-8') file.writelines(['When is the bright moon? ','Ask Qingtian about the wine. ']) file.close()

withキーワードは、ファイルを開いた後に閉じるのを忘れないように記述されています

# Common writing file = open('verse.txt','a',encoding='utf-8') file.write('When is the bright moon? ') file.close() ` # Use the wording of with keyword with open('verse.txt','a') as file: #with open('File address','Read and write mode') as Variable name: #Format: The colon cannot be lost file.write('When will the moon be there? ') #Format: Indent the file operations #Format: No need to close with close()! [open function]

ここに画像の説明を挿入してください)(https://imgconvert.csdnimg.cn/aHR0cHM6Ly9yZXNvdXJjZS5vc3MueWF0aWt1LmNvbS9wbGFuL3NoYXJlLzE1Nzg2NDAyMzI0NDMucG5n?x-os-process=image)

with open('verse.txt','r',encoding='utf-8') as file: filedata = file.readlines() for i in filedata: print(i.replace(' ',''))#Replace in the file with spaces

replace()メソッドには2つのパラメーターがあります。1つ目は置き換えられる文字で、2つ目は古い文字を置き換えるために使用される新しい文字です。