Create a CSV Text File From a Tab Delimited File using Python
In this tutorial video, I take a file downloaded form a website that is tab delimited and convert it to a comma delimited csv file using a python program that can be called from the command line.
This csv file will be imported into LibreOffice base (Free database software for Mac, PC or Linux) for further processing.
If you program or write macros in a business setting I recommend getting on top of python
This is because when you integrate Python with visual basic for applications vba (Microsoft office) or Basic as used in Open Office or LibreOffice, then you can accomplish most business process automation and reporting tasks that your business unit would ever need.
Python can be used to log into and external website and download any data that that site displays, say to a text file, this text file could then be loaded to an Access database or Excel spreadsheet, all at the click of a single button.
Don’t forget to comment below and let me know if this was helpful.
Also feel free to tell me what topics you would like covered in the future.
Saurabh says
What’s wrong with this code? Not getting common after every value in the output file.
”’
Created on May 14, 2018
@author: ssriva112
”’
import os, sys
def processFile(szPath, szFile):
os.chdir(szPath)
lstFile=szFile.split(“.”)
Fo = open(szFile, “r”, encoding=’latin-1′)
lstInput=[]
for oLine in Fo:
try:
lstLine = oLine.replace(“\n”, “”).split(“\t”)
except Exception as e:
print(e)
pass
lstInput.append(lstLine)
print(szFile +” read”)
Fo.close()
fW=open(lstFile[0] + “Output.csv”, “w”, encoding =’ascii’)
print(“Writing” + lstFile[0] + “Output.csv”)
for oLine in lstInput:
szWriteLine = “,”.join(oLine)
print(oLine)
fW.write(szWriteLine + “\n”)
processFile(sys.argv[1], sys.argv[2])