Convert txt to csv python script

后端 未结 4 914
栀梦
栀梦 2020-12-31 19:28

I have a .txt file with this inside - 2.9,Gardena CA

What I\'m trying to do is convert that text into a .csv (table) using a python script:

import c         


        
4条回答
  •  执念已碎
    2020-12-31 20:28

    This is how I do it:

     with open(txtfile, 'r') as infile, open(csvfile, 'w') as outfile:
            stripped = (line.strip() for line in infile)
            lines = (line.split(",") for line in stripped if line)
            writer = csv.writer(outfile)
            writer.writerows(lines)
    

    Hope it helps!

提交回复
热议问题