Files
MinFt/Client/Assets/StreamingAssets/csv_to_tsv.py
2026-04-27 12:07:32 +08:00

46 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""
将 boostrapi18n.csv 转换为 boostrapi18n.tsv
- 单元格内真实换行写成字面 \\r\\n制表符写成字面 \\t避免破坏列
- 不用引号包裹字段,输出无首尾 "
"""
import csv
import os
def _normalize_cell(cell):
"""还原 CSV 双引号转义;真实换行/制表符改为字面 \\r\\n、\\t避免输出时被加引号"""
if not isinstance(cell, str):
return cell
s = cell.replace('""', '"')
s = s.replace('\r\n', '\\r\\n').replace('\n', '\\r\\n').replace('\r', '\\r\\n')
s = s.replace('\t', '\\t') # 单元格内真实 tab 写成 \t避免破坏列
return s
def convert_csv_to_tsv():
csv_file = 'boostrapi18n.csv'
tsv_file = 'boostrapi18n.tsv'
if not os.path.exists(csv_file):
print(f'错误: 找不到文件 {csv_file}')
return False
try:
with open(csv_file, 'r', encoding='utf-8') as f_in:
reader = csv.reader(f_in)
rows = [[_normalize_cell(cell) for cell in row] for row in reader]
# 手动按 \t 拼接行,不经过会加引号的 writer保证无首尾 "
with open(tsv_file, 'w', encoding='utf-8', newline='') as f_out:
for row in rows:
f_out.write('\t'.join(row) + '\n')
print(f'成功: {csv_file} 已转换为 {tsv_file}')
return True
except Exception as e:
print(f'错误: 转换失败 - {str(e)}')
return False
if __name__ == '__main__':
convert_csv_to_tsv()