1. 使用`split()`函数:
text = "Hello, World!"words = text.split(", ")print(words) 输出:['Hello', 'World!']
2. 使用`re`模块中的`split()`函数:
import retext = "Hello, World!"words = re.split(",\s*", text)print(words) 输出:['Hello', 'World!']
3. 使用`str.splitlines()`函数:

text = "Hello\nWorld!"lines = text.splitlines()print(lines) 输出:['Hello', 'World!']
4. 根据文件大小分割文本文件:
import osdef split_file(filepath, block_size):filesize = os.path.getsize(filepath)blocks = math.ceil(filesize / block_size)last_block_size = block_size if filesize % block_size == 0 else filesize % block_sizewith open(filepath, 'rb') as fl:for i in range(blocks):filename = f'{filepath}.part{i}'with open(filename, 'wb') as fw:if i != blocks - 1:fw.write(fl.read(block_size))else:fw.write(fl.read(last_block_size))split_file('path_to_your_file', 1024*1024) 1MB each
5. 根据行数分割文本文件:
def split_file_by_lines(filepath, lines_per_file):with open(filepath, 'r') as file:lines = file.readlines()for i in range(0, len(lines), lines_per_file):with open(f'{filepath}_part{i//lines_per_file}.txt', 'w') as output_file:output_file.writelines(lines[i:i+lines_per_file])split_file_by_lines('path_to_your_file', 100) 100 lines per file
请根据你的具体需求选择合适的方法。
