zoukankan      html  css  js  c++  java
  • 全角半角转换

    对于小文件:

    # -*- coding: utf-8 -*-
    
    import argparse
    
    parser = argparse.ArgumentParser()
    parser.add_argument('-m', '--mode', default='f2h', help='full2half or half2full')
    parser.add_argument('-i', '--input_file', help='input filename')
    parser.add_argument('-o', '--output_file', default='output.txt', help='output filename')
    
    args = parser.parse_args()
    
    # 全角对应半角字典
    # ASCII字符
    full_to_half = {chr(i): chr(i - 0xfee0) for i in range(0xFF01, 0xFF5E + 1)}
    full_to_half[chr(0x3000)] = chr(0x20)
    
    # 半角对应全角字典
    half_to_full = dict((v, k) for k, v in full_to_half.items())
    
    
    def full2half(text):
        """全角转半角
        :param text: 输入文本
        :return: 输出文本
        """
        return ''.join([full_to_half.get(i, i) for i in text])
    
    
    def half2full(text):
        """半角转全角
        :param text: 输入文本
        :return: 输出文本
        """
        return ''.join([half_to_full.get(i, i) for i in text])
    
    
    def main():
        with open(args.input_file, 'r', encoding='utf-8') as f_in, 
                open(args.output_file, 'w', encoding='utf-8') as f_out:
            text = f_in.read()
            if args.mode == 'f2h':
                output_text = full2half(text)
            else:
                output_text = half2full(text)
            f_out.write(output_text)
    
    
    if __name__ == '__main__':
        main()

    对于大文件:

    # -*- coding: utf-8 -*-
    
    import argparse
    
    parser = argparse.ArgumentParser()
    parser.add_argument('-m', '--mode', default='f2h', help='full2half or half2full')
    parser.add_argument('-i', '--input_file', help='input filename')
    parser.add_argument('-o', '--output_file', default='output.txt', help='output filename')
    
    args = parser.parse_args()
    
    # 全角对应半角字典
    # ASCII字符
    full_to_half = {chr(i): chr(i - 0xfee0) for i in range(0xFF01, 0xFF5E + 1)}
    full_to_half[chr(0x3000)] = chr(0x20)
    
    # 半角对应全角字典
    half_to_full = dict((v, k) for k, v in full_to_half.items())
    
    
    def full2half(text):
        """全角转半角
        :param text: 输入文本
        :return: 输出文本
        """
        return ''.join([full_to_half.get(i, i) for i in text])
    
    
    def half2full(text):
        """半角转全角
        :param text: 输入文本
        :return: 输出文本
        """
        return ''.join([half_to_full.get(i, i) for i in text])
    
    
    def main():
        with open(args.input_file, 'r', encoding='utf-8') as f_in, 
                open(args.output_file, 'w', encoding='utf-8') as f_out:
            for text in f_in:
                if args.mode == 'f2h':
                    output_text = full2half(text)
                else:
                    output_text = half2full(text)
                f_out.write(output_text)
    
    
    if __name__ == '__main__':
        main()
  • 相关阅读:
    洛谷—— P3353 在你窗外闪耀的星星
    洛谷—— P1238 走迷宫
    洛谷—— P1262 间谍网络
    9.8——模拟赛
    洛谷—— P1189 SEARCH
    算法
    May 22nd 2017 Week 21st Monday
    May 21st 2017 Week 21st Sunday
    May 20th 2017 Week 20th Saturday
    May 19th 2017 Week 20th Friday
  • 原文地址:https://www.cnblogs.com/jacen789/p/10208444.html
Copyright © 2011-2022 走看看