Python Application 1 – Format File Content

 
Step 1
 
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
 
#!/usr/bin/python
#Author : Jijo K Jose
 
input_file = raw_input("\nEnter input file name : ")
output_file = "file_new.txt"
 
h1 = open(input_file,"r")
 
print "\n—————————-"
print "Input File Content"
print "—————————-\n"
 
for i in h1:
 print i,
 
h1.close()
 
h1 = open(input_file,"r")
 
h2 = open(output_file,"w")
 
k=0
 
for i in h1:
 i2 = i.split(',')
 for j in i2:
  n = len(j)
  if j[n-1] =="\n":
   h2.write(j)
  else:
   h2.write(j)
   h2.write("\n")
 
h1.close()
h2.close()
 
print "\n—————————"
print "Output File Content – file_new.txt"
print"—————————-\n"
 
h1 = open(output_file,"r")
for i in h1:
 print i,
 
print "\n—————————\n"
 
#END

 
The file contents are
 
123,45,78965,54545746423,546545423123
56,789,258,323545
2,578,3,66
5
 
Step 2
 
Change file permission to executable using chmod command.
 
Step 3
 
Execute the python file.
 
python-application-format-file
 

 
You can leave a response, or trackback from your own site.

Leave a Reply