| 159 | |
| 160 | |
| 161 | def str_full_to_half_width(input_file, output_file): |
| 162 | def process(ustring): |
| 163 | result = [] |
| 164 | for char in ustring: |
| 165 | # 全角空格直接转换为半角空格 |
| 166 | if char == "\u3000": |
| 167 | result.append(" ") |
| 168 | # 处理其他全角字符(根据Unicode范围) |
| 169 | elif "\uff01" <= char <= "\uff5e": |
| 170 | # 将全角字符的Unicode码减去偏移量0xFEE0得到半角字符 |
| 171 | result.append(chr(ord(char) - 0xFEE0)) |
| 172 | else: |
| 173 | # 保留原本是半角的字符 |
| 174 | result.append(char) |
| 175 | return "".join(result) |
| 176 | |
| 177 | # 确保输入文件存在 |
| 178 | if not os.path.exists(input_file): |
| 179 | print(f"输入文件 {input_file} 不存在!") |
| 180 | return |
| 181 | |
| 182 | try: |
| 183 | # 打开输入文件和输出文件 |
| 184 | with open(input_file, "r", encoding="utf-8") as infile, open( |
| 185 | output_file, "w", encoding="utf-8" |
| 186 | ) as outfile: |
| 187 | |
| 188 | # 按行读取输入文件 |
| 189 | for line in infile: |
| 190 | # 对当前行的内容进行全角转半角处理 |
| 191 | converted_line = process(line) |
| 192 | # 写入到输出文件 |
| 193 | outfile.write(converted_line) |
| 194 | print(f"处理完成!转换后的内容已保存到 {output_file}") |
| 195 | except Exception as e: |
| 196 | print(f"处理文件时出现错误:{e}") |
| 197 | |
| 198 | def file_split(input_file, num): |
| 199 | # 假设你的文件名是 'data.txt' |