regex 加速正则表达式并使其更快的方法

rqenqsqc  于 2023-03-31  发布在  其他
关注(0)|答案(1)|浏览(101)

有没有方法可以加速这个代码regex代码?文件真的很大,因为大小的原因不会在excel中打开。

import regex as re

path = "C:/Users/.../CDPH/"
with open(path + 'Thefile.tab') as file:
     data = file.read()
     # replace all space bars between tab characters
     data = re.sub('( )*(?=\n)|( )*(?=\t)', '', data )
with open(path + 'Data.csv', 'w') as file:
     file.write(data)
wztqucjr

wztqucjr1#

由于不知道制表符分隔的csv文件的确切方言,我不得不猜测一下,你会在csv库文档中找到更多的选项。
以下是我试图加快正确修剪字段的方法:

#!/usr/bin/python

import csv

with open('Data.csv', 'w', newline='') as outfile:
    with open('Thefile.tab', newline='') as infile:
        rd = csv.reader(infile, delimiter = '\t')
        wr = csv.writer(outfile, delimiter = '\t')
        for row in rd:
            row = [field.rstrip() for field in row]
            wr.writerow(row)

相关问题