正则替换中文字符串中英文前后的空格

import csv
import re

pattern = r'(?<=[\u4e00-\u9fa5])\s+(?=[^\u4e00-\u9fa5])|(?<=[^\u4e00-\u9fa5])\s+(?=[\u4e00-\u9fa5])'
results = []

with open('vl6_20.csv', 'r', encoding="utf-8") as file:
    reader = csv.reader(file)
    for row in reader:
        tm = ''
        tm = re.sub(pattern, '', row[2])
        tm = tm.replace('。 ', '。')
        tm = tm.replace(', ',',')
        tm = tm.replace('; ',';');
        tm = tm.replace(', ',',');
        tm = tm.replace('. ','.');
        tm = tm.replace('; ',';');
        row[2] = tm
        results.append(row)


with open('vl6_20-ok.csv', 'w', newline='', encoding="utf-8") as file2:
    writer = csv.writer(file2)
    writer.writerows(results)

你可能感兴趣的:(python,python)