First Python Assignment ..Help Me out!

This is the place for queries that don't fit in any of the other categories.

First Python Assignment ..Help Me out!

Postby perelesnyk » Sat Jan 25, 2014 7:24 pm

OK, so my prof is asking me to run before I crawl.. This is my first attempt at Python, and I have NO experience coding anything except a tiny bit of html and VB.(2 assignments worth)
I am really struggling with the basics, and I`m overwhelmed with information. I have to convert a list like:

Code: Select all
Person     Residence
1            14 Blue Jay Ave, NF, Ny, USA,14606
2            101 Hamburg St, S, Tucsan, AZ, USA, 17374
3            2342 Illinois Ave, Las Vegas, Neveda, 34223
.... 91


Code: Select all
Person     StreetNum   StreetName    City     State      Country     Zip
1             14               Blue Jay Ave   NF         NY          etc..
.... 91

I have attempted to watch tutorials and do exercises, and I've pieced together a little.. still really struggling though and running out of time. Very Frustrated new user! I`m confused as I want to use loops to cycle through 91 lines of addresses, yet do I need this as some functions automatically cycle through items in a list....I`m lost... I know it's simple, I just don't know how to say it. My code thus far is:

Code: Select all
def  retrieveFile():
       d1RawListOfAddresses = {}
       RawListOfAddressesStr = 'Person\n\nAddress'

       inFile = open("C:/Data/d1RawListOfAddresses.txt")

   except(IOError), e:
       print "File not found", e

       for line in fob:
           Person, Address = line.split()
           Person = [Person]

       for i in sorted(Person.keys(), reverse=False):
      print (Person[i])
           print '\n'

       print PersonStr

   outFile= open("RawDataResults.txt", "w")

       print "Addresses Finished Output"

    def main():

if __d1RawDataResults__ == '__main__': main()

I keep getting syntrax errors, indent errors, and I`m sure my code is incomplete to say the least

I`ll keep working at it.. can anyone help out or point me to `non-confusing`` resources on the web... I am not a natural by far, and this stuff doesn`t make lots of sense to me....
Last edited by stranac on Sat Jan 25, 2014 8:11 pm, edited 1 time in total.
Reason: Added code tags. Locked.
Posts: 1
Joined: Sat Jan 25, 2014 7:01 pm

Re: First Python Assignment ..Help Me out!

Postby stranac » Sat Jan 25, 2014 9:56 pm

That looks like a really weird format for the file to be in(sometimes there are spaces, sometimes not, multiple spaces separating fields, etc...).
Makes it hard to work with.
If the format made more sense, you could just use the csv module.

One approach is to just use str.split() like you were doing - note that it takes an optional argument.
You would do something like this to get all the parts:
Code: Select all
>>> line = '1            14 Blue Jay Ave, NF, Ny, USA,14606'
>>> person, residence = line.split(None, 1)
>>> person
>>> residence
'14 Blue Jay Ave, NF, Ny, USA,14606'
>>> address, city, state, country, zip = residence.split(',')
>>> address
'14 Blue Jay Ave'
>>> city
' NF'
>>> state
' Ny'
>>> country
' USA'
>>> street_number, street_name = address.split(' ', 1)
>>> street_number
>>> street_name
'Blue Jay Ave'

If you mind the extra whitespace, use str.strip() to get rid of it.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 6 guests