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 0000000..cc8104e Binary files /dev/null and b/2025.08.26_data_file_format/data.db differ 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 0000000..731dae4 Binary files /dev/null and b/2025.08.26_data_file_format/data.h5 differ diff --git a/2025.08.26_data_file_format/data.json b/2025.08.26_data_file_format/data.json new file mode 100644 index 0000000..c0ad850 --- /dev/null +++ b/2025.08.26_data_file_format/data.json @@ -0,0 +1,8 @@ +{ + "name": "Guan", + "year": 1993, + "hobbies": [ + "coding", + "reading" + ] +} \ No newline at end of file diff --git a/2025.08.26_data_file_format/data.pkl b/2025.08.26_data_file_format/data.pkl new file mode 100644 index 0000000..1c6c4cc Binary files /dev/null and b/2025.08.26_data_file_format/data.pkl differ diff --git a/2025.08.26_data_file_format/data.txt b/2025.08.26_data_file_format/data.txt new file mode 100644 index 0000000..6b4a2fb --- /dev/null +++ b/2025.08.26_data_file_format/data.txt @@ -0,0 +1,2 @@ +test_1 +test_2 \ No newline at end of file