将txt转换为CSV,并按列分隔

问题描述

我有一个文件夹,其中所有.txt文件都具有相同格式,但制表符分开。我正在尝试将它们转换为以列分隔的csv。

我尝试了一个简单的add_url_rule

但是它并没有做我要寻找的分离。任何建议都将受到欢迎。谢谢!

解决方法

尝试这样的事情:

import os
import pandas as pd

for filename in os.listdir('path/to/dir/'):
    if filename.endswith('.txt'):
        df = pd.read_table(filename,sep='\t',header=None) # header=None becuase you didn't say that it was data,if it is data just remove this.
        df.to_csv(f'{filename[:-3]}csv',index=False)