Saturday, April 16, 2022

[SOLVED] How to Convert text file to CSV in python

Issue

I need to convert text file to csv file.Below command is not working in python, which is working fine in shell prompt.Please help me to fix the issue.

subprocess.call("sed 's/[[:space:]]\+/,/g' test.txt > test.csv")

Content of test.txt

jega mca
divya BE
ramya MA

Solution

Add shell=True

subprocess.call("sed 's/[[:space:]]\+/,/g' test.txt > test.csv", shell=True) 


Answered By - Jean Bouvattier
Answer Checked By - Mary Flores (WPSolving Volunteer)