Python Code that reads a set of text file(.txt) and Converts it to .CSV files(Comma Separated Values)

Please, Understand it.


import os
import re
import csv

#The imported csv is  the module for conversion  
path = 'insert path to the files you intend to get it's csv format'
listing = os.listdir(path)
for infile in listing:
    dir_item_path = os.path.join(path, infile)
    fh = open(dir_item_path,'r')
    Fname = infile
    Lname = Fname.split('.')[0]
    name = Lname + ".csv"
    process_file = open(name,"wb")
    newfile = csv.writer(process_file)
    #Note: in the above under the for loop, is for setting up the file to  write into.
    for line in fh:
         space_remove = re.sub(r"\s+",",",line.rstrip())
        #re.sub is a regular expression that substitutes any space with a comma
         split_Line = space_remove.split(",")
         newfile.writerow(split_Line)
    process_file.close()


HOPE, THIS WILL BE HELPFUL TO U!!!!!!!



For ur questions u can comment on this post, or send me emails on anikenneth333@gmail.com.

I Love U.

Python Code that reads a set of text file(.txt) and Converts it to .CSV files(Comma Separated Values) Python Code that reads a set of text file(.txt) and Converts it to .CSV files(Comma Separated Values) Reviewed by Unknown on 08:18 Rating: 5

No comments:

Powered by Blogger.