User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 403,516 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,893 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 214 | Replies: 6
Reply
Join Date: Jul 2008
Posts: 7
Reputation: 2ashwinkulkarni is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
2ashwinkulkarni 2ashwinkulkarni is offline Offline
Newbie Poster

Need help with confusing issue !!

  #1  
Jul 16th, 2008
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


for i in range(0,num):
    #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()
Last edited by Tekmaven : Jul 17th, 2008 at 4:24 am. Reason: Code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Posts: 1,243
Reputation: sneekula is on a distinguished road 
Rep Power: 4
Solved Threads: 39
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Virtuoso

Re: Need help with confusing issue !!

  #2  
Jul 16th, 2008
Please learn how to use code tags around your code, nobody will read the ugly mess you are presenting otherwise.
No one died when Clinton lied.
Reply With Quote  
Join Date: Jul 2008
Posts: 7
Reputation: 2ashwinkulkarni is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
2ashwinkulkarni 2ashwinkulkarni is offline Offline
Newbie Poster

Re: Need help with confusing issue !!

  #3  
Jul 17th, 2008
Originally Posted by sneekula View Post
Please learn how to use code tags around your code, nobody will read the ugly mess you are presenting otherwise.



No problem, I just put everything together. Let me try doing it again.......

import sys
import os


choice = raw_input("Please choose type of file you want to categorize(abc, cde):")

inFile = raw_input("\nPlease Enter the complete path for the input file including the file extension")

inputFile = open(inFile, "r")

coin = raw_input("\nPlease enter the Coin or COINS; seperated by space: according to which you want to categorize the file:")

coinNameforFile = coin.replace('/','_')

coinNameforFileList = coinNameforFile.split()

num = len(coinNameforFileList)

coinList = coin.split()


# This is where the problem starts
for i in range(0,num):
    
    fileName = choice + coinNameforFileList[i]
    
    outFile = os.path.join("c:\\Documents and Settings\\......\\Desktop\\", "%s.csv" %fileName)
    
    outputFile = open(outFile, "w")


    if choice == "abc":
        outputFile.write("# Node Export file by DS-TE Import/Export module \n# Locale is optional in import\
        and mandatory in export.\n locale : C \n# Router_Name, Router_Model, Longitude (X), Latitude (Y), Bw_Constraint_Model\n")

       
    if choice == "cde":
        outputFile.write("# Node Export file by DS-TE Import/Export module \n# Locale is optional in import\
        and mandatory in export.\n locale : C \n# Link_Name, Link_Model, Router_A, Router_B, Data_Rate, Direction\n")

        
   
    inputFile.readline()
   
    resultsList = []
        
   
    if choice == "LinkNode":
         for line in inputFile:
            line = line.rstrip() 
            fields = line.split(',') 

            Link_Name,Link_Model,Router_A,Router_B,Data_Rate,Direction,COIN=fields
            
            for item in fields:
                if item == coinList[i]:
                        print item
                        resultsList = ([Link_Name, ",", Link_Model,",", Router_A, ",", Router_B, ",", Data_Rate, ",", Direction, "\n"])
                        

                        outStr ='  '.join([''.join(item) for item in resultsList])

                        outputFile.write(outStr)

                        print outStr

    
    if choice == "CellSite":
        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

            CELL_NAME, SWITCH, SWITCH_NAME, LATITUDE_DEGREES, LONGITUDE_DEGREES, COINS=fields
            

            for item in fields:
                if item == coinList[i]:
                    print item
                    resultsList = ([CELL_NAME,",","umts_node_b",",",LONGITUDE_DEGREES,",", LATITUDE_DEGREES,"\n"])
                    
                    outStr ='  '.join([''.join(item) for item in resultsList])

                    outputFile.write(outStr)

                    print outStr
        

#clean close the input and output files
inputFile.close()
outputFile.close()

Let me know if this is readable !!!
As I said, am new to the whole programming domain and am unsure of which snippet to post.
Last edited by Tekmaven : Jul 17th, 2008 at 4:24 am. Reason: Code tags
Reply With Quote  
Join Date: Jun 2008
Posts: 32
Reputation: AceofSpades19 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 5
AceofSpades19's Avatar
AceofSpades19 AceofSpades19 is offline Offline
Light Poster

Re: Need help with confusing issue !!

  #4  
Jul 17th, 2008
No, put code tags around the code in your post eg.
[code ]
#python code here
[/code ]
Reply With Quote  
Join Date: Jul 2008
Posts: 7
Reputation: 2ashwinkulkarni is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
2ashwinkulkarni 2ashwinkulkarni is offline Offline
Newbie Poster

Re: Need help with confusing issue !!

  #5  
Jul 17th, 2008
Thnx for bearing with me !!! first time posting you see !!!!!!

Hope this is the right way to do it.

import sys
import os


choice = raw_input("Please choose type of file you want to categorize(abc, cde):")

inFile = raw_input("\nPlease Enter the complete path for the input file including the file extension")

inputFile = open(inFile, "r")

coin = raw_input("\nPlease enter the Coin or COINS; seperated by space: according to which you want to categorize the file:")

coinNameforFile = coin.replace('/','_')

coinNameforFileList = coinNameforFile.split()

num = len(coinNameforFileList)

coinList = coin.split()


# This is where the problem starts
for i in range(0,num):
    
    fileName = choice + coinNameforFileList[i]
    
    outFile = os.path.join("c:\\Docum....\\......\\Desktop\\", "%s.csv" %fileName)
    
    outputFile = open(outFile, "w")


    if choice == "abc":
        outputFile.write("# Node Export file by DS-TE Import/Export module\n")

       
    if choice == "cde":
        outputFile.write("# Node Export file by DS-TE Import/Export module \n")

        
   
    inputFile.readline()
   
    resultsList = []
        
   
    if choice == "LinkNode":
         for line in inputFile:
            line = line.rstrip() 
            fields = line.split(',') 

            Link_Name,Link_Model,Router_A,Router_B,Data_Rate,Direction,COIN=fields
            
            for item in fields:
                if item == coinList[i]:
                        print item
                        resultsList = ([Router_A, ",", Router_B, ",", Data_Rate, ",", Direction, "\n"])
                        

                        outStr ='  '.join([''.join(item) for item in resultsList])

                        outputFile.write(outStr)

                        print outStr

    
    if choice == "CellSite":
        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

            CELL_NAME, SWITCH, SWITCH_NAME, LONG, LAT, COINS=fields
            

            for item in fields:
                if item == coinList[i]:
                    print item
                    resultsList = ([CELL_NAME,",",LONGITUDE,",", LATITUDE_DEGREES,"\n"])
                    
                    outStr ='  '.join([''.join(item) for item in resultsList])

                    outputFile.write(outStr)

                    print outStr
        

#clean close the input and output files
inputFile.close()
outputFile.close()

Reply With Quote  
Join Date: Jan 2008
Posts: 538
Reputation: ZZucker is on a distinguished road 
Rep Power: 2
Solved Threads: 15
ZZucker's Avatar
ZZucker ZZucker is offline Offline
Posting Pro

Re: Need help with confusing issue !!

  #6  
Jul 17th, 2008
There are some dangling lines in your code like:
Link_Name,Link_Model,Router_A,Router_B,Data_Rate,Direction,COIN=fields

or

CELL_NAME, SWITCH, SWITCH_NAME, LONG, LAT, COINS=fields

Are those comments? If yes, they need a # in front.
Never argue with idiots, they'll just bring you down to their level and beat you with their experience.
Reply With Quote  
Join Date: Jul 2008
Posts: 7
Reputation: 2ashwinkulkarni is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
2ashwinkulkarni 2ashwinkulkarni is offline Offline
Newbie Poster

Re: Need help with confusing issue !!

  #7  
Jul 17th, 2008
Actually no, They are not! its just that they are longer than the rest and so they continue on the next line without indentation
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 1:02 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC