Processing a comma-delimited text file. [solved]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mandrah
    New Member
    • Nov 2006
    • 3

    #1

    Processing a comma-delimited text file. [solved]

    I have a single text file with several lines of information with three items separated by commas. An example of a few lines:

    name1, email1, password1
    name2, email2, password2
    name3, email3, password3

    Using:
    Code:
    def main():
        f = open("database.txt","r")
        print f.readlines()
    main()
    the program prints a list similar to:

    ['name1, email1, password1\n', 'name2, email2, password2\n', 'name3, email3, password3\n']

    A list delimited by the new line character?

    My question is: how can I read in this text file with the same format but instead, make a list where each element is delimited by the comma between each word and the new line at the end of each line.

    Also is there are way to pass this into a dictionary instead of a list, using 3 keys ("name" "email" "password") and the corresponding values in the text file?
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Here's one way (quick and dirty):
    Code:
    column_header = ['name', 'email', 'password']
    dbList = []
    def main():
    	f = open("database.txt","r")
    	for line in f:
    		entryDict = {}
    		tempList = line.split(',')
    		for i, header in enumerate(column_header):
    			entryDict[header] = tempList[i].strip()
    		dbList.append(entryDict)
    	f.close()
    That should give you enough usage to get you started. Please keep posting to the python forum,
    Barton

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bartonc
      Here's one way (quick and dirty):
      Code:
      column_header = ['name', 'email', 'password']
      dbList = []
      def main():
      	global dbList
      	f = open("database.txt","r")
      	for line in f:
      		entryDict = {}
      		tempList = line.split(',')
      		for i, header in enumerate(column_header):
      			entryDict[header] = tempList[i].strip()
      		dbList.append(entryDict)
      	f.close()
      main()
      print dbList[0]
      That should give you enough usage to get you started. Please keep posting to the python forum,
      Barton
      Ok, this works.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Actually, it works either way.

        Comment

        • Mandrah
          New Member
          • Nov 2006
          • 3

          #5
          That line.split() was what I was looking for. Thank you!

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by Mandrah
            That line.split() was what I was looking for. Thank you!
            You are quite welcome. Keep posting,
            Barton

            Comment

            Working...