簡體   English   中英

如何從 csv 文件中快速批量刪除羅馬數字

[英]How to quickly batch remove roman numerals from csv file

目前,我正在使用以下 Python 代碼刪除一些 csv 文件中的羅馬數字:

def inplace_change(filename, old_string, new_string):
    # Safely read the input filename using 'with'
    with open(filename) as f:
        s = f.read()
        if old_string not in s:
            print('"{old_string}" not found in {filename}.'.format(**locals()))
            return

    # Safely write the changed content, if found in the file
    with open(filename, 'w') as f:
        print('Changing "{old_string}" to "{new_string}" in {filename}'.format(**locals()))
        s = s.replace(old_string, new_string)
        f.write(s)

d_list = ['1. ', '2. ', '3. ', '4. ','XVIII. ','XVII. ','XVI. ','XV. ','XIV. ', 'XIII. ', 
          'XII. ','XI. ', 'IX. ','VIII. ', 'VII. ', 'VI. ','IV. ', 'IV. ', 'XVIII.','XVII.','XVI.','XV.','XIV.', 'XIII.', 
          'XII.','XI.', 'IX.','VIII.', 'VII.', 'VI.','IV.', 'IV.', 'Ⅰ.', 'Ⅱ.','Ⅲ.','Ⅳ.','Ⅴ.','Ⅵ.','Ⅶ-1.','Ⅶ-2.','Ⅶ.','Ⅱ.'
           'Ⅷ.','Ⅸ.','Ⅹ.','1.','2.','3.','4.','5.','6.','7.',
         'I. ','II. ','III. ','Ⅷ.',
         'ⅥI. ',  'VIIII. ',  '- ',  'I',  'II',
          'V.',  'Ⅵ',  'VIII',  'I.',  'II.',
          'V.',  'X.',  'Ⅹ',  'V',  'Ⅷ.',
         ]
for file in os.listdir(output_path + '/CIS'): 
    for dlist in d_list:    
        inplace_change(output_path +'/CIS/'+ file,  old_string= dlist, new_string= '')  
        continue

但是,處理速度太慢,這是一個問題。 有沒有更快更方便的方法?

根據我從您的代碼中了解到的情況,在d_list的每次迭代中,您都必須打開文件。 所以這個過程效率不高,因為每次讀/寫文件都會花費一些時間。

打開一個文件,然后遍歷d_list怎么樣? 例如:

new_string= ''
d_list = ['1. ', '2. ', '3. ', '4. ','XVIII. ','XVII. ','XVI. ','XV. ','XIV. ', 'XIII. ', 
          'XII. ','XI. ', 'IX. ','VIII. ', 'VII. ', 'VI. ','IV. ', 'IV. ', 'XVIII.','XVII.','XVI.','XV.','XIV.', 'XIII.', 
          'XII.','XI.', 'IX.','VIII.', 'VII.', 'VI.','IV.', 'IV.', 'Ⅰ.', 'Ⅱ.','Ⅲ.','Ⅳ.','Ⅴ.','Ⅵ.','Ⅶ-1.','Ⅶ-2.','Ⅶ.','Ⅱ.'
           'Ⅷ.','Ⅸ.','Ⅹ.','1.','2.','3.','4.','5.','6.','7.',
         'I. ','II. ','III. ','Ⅷ.',
         'ⅥI. ',  'VIIII. ',  '- ',  'I',  'II',
          'V.',  'Ⅵ',  'VIII',  'I.',  'II.',
          'V.',  'X.',  'Ⅹ',  'V',  'Ⅷ.',
         ]

def inplace_change(filename):
    # Safely read the input filename using 'with'
    with open(filename, 'r+') as f:    #'r+' is for read and write
        s = f.read()
        for old_string in d_list: 
            if old_string not in s:
                print(f'"{old_string}" not found in {filename}.')
            else:
                # Safely write the changed content, if found in the file
                f.seek(0)    #IMPORTANT, to bring the cursor to the beginning of the file
                print(f'Changing "{old_string}" to "{new_string}" in {filename}')
                s = s.replace(old_string, new_string)
                f.write(s)

for file in os.listdir(output_path + '/CIS'): 
    inplace_change(output_path +'/CIS/'+ file) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM