From b411789fd35902a4f8bbefe8544bec59daed8f03 Mon Sep 17 00:00:00 2001 From: guanjihuan Date: Tue, 26 Aug 2025 17:30:04 +0800 Subject: [PATCH] update --- 2025.08.26_data_file_format/CSV_example.py | 14 ++++++++++ 2025.08.26_data_file_format/HDF5_example.py | 16 +++++++++++ 2025.08.26_data_file_format/JSON_example.py | 17 ++++++++++++ 2025.08.26_data_file_format/Pickle_example.py | 12 ++++++++ 2025.08.26_data_file_format/SQLite_example.py | 26 ++++++++++++++++++ 2025.08.26_data_file_format/TXT_example.py | 14 ++++++++++ 2025.08.26_data_file_format/data.csv | 3 ++ 2025.08.26_data_file_format/data.db | Bin 0 -> 8192 bytes 2025.08.26_data_file_format/data.h5 | Bin 0 -> 6216 bytes 2025.08.26_data_file_format/data.json | 8 ++++++ 2025.08.26_data_file_format/data.pkl | Bin 0 -> 22 bytes 2025.08.26_data_file_format/data.txt | 2 ++ 12 files changed, 112 insertions(+) create mode 100644 2025.08.26_data_file_format/CSV_example.py create mode 100644 2025.08.26_data_file_format/HDF5_example.py create mode 100644 2025.08.26_data_file_format/JSON_example.py create mode 100644 2025.08.26_data_file_format/Pickle_example.py create mode 100644 2025.08.26_data_file_format/SQLite_example.py create mode 100644 2025.08.26_data_file_format/TXT_example.py create mode 100644 2025.08.26_data_file_format/data.csv create mode 100644 2025.08.26_data_file_format/data.db create mode 100644 2025.08.26_data_file_format/data.h5 create mode 100644 2025.08.26_data_file_format/data.json create mode 100644 2025.08.26_data_file_format/data.pkl create mode 100644 2025.08.26_data_file_format/data.txt diff --git a/2025.08.26_data_file_format/CSV_example.py b/2025.08.26_data_file_format/CSV_example.py new file mode 100644 index 0000000..2d61d1e --- /dev/null +++ b/2025.08.26_data_file_format/CSV_example.py @@ -0,0 +1,14 @@ +import csv + +# 写入 CSV +with open('data.csv', 'w', newline='', encoding='utf-8') as f: + writer = csv.writer(f) + writer.writerow(['Name', 'year', 'value']) # 写入表头 + writer.writerow(['Guan', 1993, 1]) + writer.writerow(['G123', 2025, 0]) + +# 读取 CSV +with open('data.csv', 'r', encoding='utf-8') as f: + reader = csv.reader(f) + for row in reader: + print(row) # 每行是一个列表 \ No newline at end of file diff --git a/2025.08.26_data_file_format/HDF5_example.py b/2025.08.26_data_file_format/HDF5_example.py new file mode 100644 index 0000000..4f17b91 --- /dev/null +++ b/2025.08.26_data_file_format/HDF5_example.py @@ -0,0 +1,16 @@ +import h5py +import numpy as np + +# 写入数据 +with h5py.File('data.h5', 'w') as f: + f.create_dataset('array', data=np.random.rand(3, 3)) + f.attrs['info'] = '3x3随机矩阵' + +# 读取数据 +with h5py.File('data.h5', 'r') as f: + array_data = f['array'][:] + info = f.attrs['info'] + +print('数组数据:') +print(array_data) +print('信息:', info) \ No newline at end of file diff --git a/2025.08.26_data_file_format/JSON_example.py b/2025.08.26_data_file_format/JSON_example.py new file mode 100644 index 0000000..3610c58 --- /dev/null +++ b/2025.08.26_data_file_format/JSON_example.py @@ -0,0 +1,17 @@ +import json + +data = { + "name": "Guan", + "year": 1993, + "hobbies": ["coding", "reading"], +} + +# 写入 +with open('data.json', 'w', encoding='utf-8') as f: + json.dump(data, f, indent=4) # indent 使输出格式化,更易读 + +# 读取 +with open('data.json', 'r', encoding='utf-8') as f: + loaded_data = json.load(f) + print(loaded_data) + print(loaded_data['name']) \ No newline at end of file diff --git a/2025.08.26_data_file_format/Pickle_example.py b/2025.08.26_data_file_format/Pickle_example.py new file mode 100644 index 0000000..0b1984f --- /dev/null +++ b/2025.08.26_data_file_format/Pickle_example.py @@ -0,0 +1,12 @@ +import pickle + +data = [1, 2, 3] + +# 写入 +with open('data.pkl', 'wb') as f: # 注意是 'wb' 二进制写入 + pickle.dump(data, f) + +# 读取 +with open('data.pkl', 'rb') as f: # 注意是 'rb' 二进制读取 + loaded_data = pickle.load(f) + print(loaded_data) \ No newline at end of file diff --git a/2025.08.26_data_file_format/SQLite_example.py b/2025.08.26_data_file_format/SQLite_example.py new file mode 100644 index 0000000..89e464f --- /dev/null +++ b/2025.08.26_data_file_format/SQLite_example.py @@ -0,0 +1,26 @@ +import sqlite3 + +# 1. 连接数据库(自动创建) +conn = sqlite3.connect('data.db') +cursor = conn.cursor() + +# 2. 创建表 +cursor.execute('CREATE TABLE IF NOT EXISTS users (name TEXT, age INTEGER)') + +# 3. 插入数据 +cursor.execute("INSERT INTO users VALUES ('张三', 25)") +cursor.execute("INSERT INTO users VALUES ('李四', 30)") + +# 4. 提交更改 +conn.commit() + +# 5. 读取数据 +cursor.execute('SELECT * FROM users') +rows = cursor.fetchall() + +print("所有用户数据:") +for row in rows: + print(f"姓名: {row[0]}, 年龄: {row[1]}") + +# 6. 关闭连接 +conn.close() \ No newline at end of file diff --git a/2025.08.26_data_file_format/TXT_example.py b/2025.08.26_data_file_format/TXT_example.py new file mode 100644 index 0000000..f2f0de3 --- /dev/null +++ b/2025.08.26_data_file_format/TXT_example.py @@ -0,0 +1,14 @@ +# 写入 +with open('data.txt', 'w', encoding='utf-8') as f: + f.write('test_1\ntest_2') + +# 读取 +with open('data.txt', 'r', encoding='utf-8') as f: + content = f.read() # 读取全部内容 +print(content) + +with open('data.txt', 'r', encoding='utf-8') as f: + lines = f.readlines() # 或按行读取 +print(lines) +for line in lines: + print(line.strip()) # strip() 去除首尾空白字符 \ No newline at end of file diff --git a/2025.08.26_data_file_format/data.csv b/2025.08.26_data_file_format/data.csv new file mode 100644 index 0000000..4ff2f7e --- /dev/null +++ b/2025.08.26_data_file_format/data.csv @@ -0,0 +1,3 @@ +Name,year,value +Guan,1993,1 +G123,2025,0 diff --git a/2025.08.26_data_file_format/data.db b/2025.08.26_data_file_format/data.db new file mode 100644 index 0000000000000000000000000000000000000000..cc8104ef4939ac2a6c8fdbc70c50c5042b48d620 GIT binary patch literal 8192 zcmeI#&k8|76bA4!Ll#Qc?shjiOG-9gz@UO>uYSnxK;19$*) zZ}zfO-`C7JXQo;GcGK^+r;*|Day1Vku2POnd26pzeQ4Iv&-36 z!nu-N2m~Mi0SG_<0uX=z1Rwwb2tWV=zY6#w_XeloD8~Q* literal 0 HcmV?d00001 diff --git a/2025.08.26_data_file_format/data.h5 b/2025.08.26_data_file_format/data.h5 new file mode 100644 index 0000000000000000000000000000000000000000..731dae42f049a878ed49ab357d8de813239bb1f5 GIT binary patch literal 6216 zcmeD5aB<`1lHy_j0S*oZ76t(@6Gr@p0uKp@2#gPtPk=HQp>zk7Ucm%mFfuSRfaIXs zfu=*uuV979CqO8MkRVrA2B1oq`7jzP&0xR+VR66&M(Gh10v@i80U!@YKz$8Mr6-{2 z3>FxPMMa5~Kn^TDdcY*eq!}4lA!b1hWMt3)o5aWj5nzUzi<_1J>lak8XMm(#2L=;v z29y6#4Xi*>kV#BT5Cb`&Ifa=Ks+