使用python将批处理文本批处理为csv

使用python将批处理文本批处理为csv,第1张

使用python将批处理文本批处理为csv

glob.glob()非常适合该任务。另外,

with
在处理文件时,请使用上下文管理器

import csvimport globimport osdirectory = raw_input("INPUT Folder:")output = raw_input("OUTPUT Folder:")txt_files = os.path.join(directory, '*.txt')for txt_file in glob.glob(txt_files):    with open(txt_file, "rb") as input_file:        in_txt = csv.reader(input_file, delimiter='=')        filename = os.path.splitext(os.path.basename(txt_file))[0] + '.csv'        with open(os.path.join(output, filename), 'wb') as output_file: out_csv = csv.writer(output_file) out_csv.writerows(in_txt)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5673501.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存