Hello all,
I am new to python programming and the Qs might seem stupid but i would really appreciate some help :-)
It is just a for loop that i want to use, but it doesnt seem to work properly.
I need to sort a file as per one of the column values and store them into different files which are named acc to the sorting criteria. I created a list and am using a loop to sort everything at one go.
The Loop works fine for the first cycle, but for the next cycles the output files are getting created but they are not getting populated. The first cycle creates, sorts and populates the output file.2
here is the code snippet
import sys
import os
#Get the type and path of the input file from the user
choice = raw_input("Please choose type of file you want to categorize")
inFile = raw_input("\nPlease Enter the complete path for the input file including the file extension \
(Ex: C:\Documents and Settings\...\Desktop\python\FILENAME.csv):")
inputFile = open(inFile, "r")
# Get the name of Coin from the user
coin = raw_input("\nPlease enter the Coin or COINS; seperated by space: according to which you want to categorize the file:")
coinNameforFile = coin.replace('/','_') #we need to replace '/' with '_' for using it in the name of the file
coinNameforFileList = coinNameforFile.split()
num = len(coinNameforFileList)
coinList = coin.split()
print coin
print coinNameforFile
[B]for i in range(0,num):[/B]
#assign the name and path of the output file to be generated
fileName = choice + coinNameforFileList[i]
outFile = os.path.join("c:\\Documents and Settings\\Ashwin Kulkarni\\Desktop\\", "%s.csv" %fileName)
print "\nYour Output File name is :: %s\n" %outFile
#Open the output file for writing
outputFile = open(outFile, "w")
if choice == "abc":
outputFile.write(" some text which prepopulates the files ")
print " above text has been written to the file"
if choice == "cde":
outputFile.write("some text which prepopulates the files")
print "above text has been written to the file"
#######################################################################################
#
# Now that we have the Input and the output file ready, read the input file and
# manipulate the file as per the users requirement.
#
#######################################################################################
#start reading the input file line by line
inputFile.readline()
#define a output array
resultsList = []
########################################################################################
# categorize the input file as per the choice made by the user.
########################################################################################
if choice == "abc":
for line in inputFile:
line = line.rstrip() # Strip the cell Values of the trailing spaces
fields = line.split(',') #Seperate the values in fields with a comma
# Assign the fields present in the input file to fields
a,b,c,d,e,f,g=fields
#print fields
for item in fields:
if item == coinList[i]:
print item
resultsList = ([a,b,c,d,e,f "\n"])
#print resultsList
#Output the results in required format
outStr =' '.join([''.join(item) for item in resultsList])
#write the results to the specified file
outputFile.write(outStr)
#print the output to standard output for review
print outStr
#clean close the input and output files
inputFile.close()
outputFile.close()