Python Forum
attribute error stumped on how to fix it.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
attribute error stumped on how to fix it.
#1
Hi, Im trying to run some code but i get :
line 6, in <module>
for line in employees.splitlines():
AttributeError: '_io.TextIOWrapper' object has no attribute 'splitlines'

confPack = open("confPack.txt", "r")
cPack = list()
for eachLine in confPack:
    cPack.append(eachLine)
employees = open("employees.txt", "r")
for line in employees.splitlines():
    Attendee = line.split(",") + ['','']
    surname = Attendee[0]
    firstName = Attendee[1]
    print("Attendee: " + surname + ", " + firstName)
    if Attendee[2] == "Y" and Attendee[3] != "Y":
        print("Pack/s: " + cPack[0])
    if Attendee[3] == "Y" and Attendee[2] != "Y":
        print("Pack/s: " + cPack[0])
    if Attendee[2] == "Y" and Attendee[3] == "Y":
        print("Pack/s: " + cPack[0] + ", " + cPack[1])
    if Attendee[2] == "" and Attendee[3] == "":
        print("Pack/s: No Packs received" )
any help is much apreciated.
Reply
#2
I think you're getting splitlines() and readlines() confused. There is a splitlines(), but it's a str method, not a file-type method.
Reply
#3
(Aug-10-2020, 06:47 AM)bowlofred Wrote: I think you're getting splitlines() and readlines() confused. There is a splitlines(), but it's a str method, not a file-type method.

You nailed it, thanks alot!
Reply
#4
my last hurdle is getting the formatting correct.
the output should look like this:

Report date: 10/08/2020
Attendee: Williams, Mary
Pack/s: Basic conference pack
Attendee: Nguyen, Vinh
Pack/s: Basic conference pack
Attendee: Kingsley, Margret
Pack/s: No Packs received
Attendee: Kline, Bob
Pack/s: Basic conference pack, Bonus conference pack
Attendee: Mitchell, Frank
Pack/s: Basic conference pack
Attendee: Lowe, Elizabeth
Pack/s: Basic conference pack, Bonus conference pack

but im getting:
Report date: 10/08/2020
Attendee: Williams, Mary
Attendee: Nguyen, Vinh
Attendee: Kingsley, Margret

Pack/s: No Packs received
Attendee: Kline, Bob
Attendee: Mitchell, Frank
Attendee: Lowe, Elizabeth
Pack/s: Basic conference pack
, Bonus conference pack

the two txt files are

confpack:
Basic conference pack
Bonus conference pack

and

employees:
Williams,Mary,Y
Nguyen,Vinh,Y
Kingsley,Margret
Kline,Bob,Y,Y
Mitchell,Frank,Y
Lowe,Elizabeth,Y,Y

something in my first two if statements is wrong but i'm at a loss.

(Aug-10-2020, 11:50 AM)hank4eva Wrote: my last hurdle is getting the formatting correct.
the output should look like this:

Report date: 10/08/2020
Attendee: Williams, Mary
Pack/s: Basic conference pack
Attendee: Nguyen, Vinh
Pack/s: Basic conference pack
Attendee: Kingsley, Margret
Pack/s: No Packs received
Attendee: Kline, Bob
Pack/s: Basic conference pack, Bonus conference pack
Attendee: Mitchell, Frank
Pack/s: Basic conference pack
Attendee: Lowe, Elizabeth
Pack/s: Basic conference pack, Bonus conference pack

but im getting:
Report date: 10/08/2020
Attendee: Williams, Mary
Attendee: Nguyen, Vinh
Attendee: Kingsley, Margret

Pack/s: No Packs received
Attendee: Kline, Bob
Attendee: Mitchell, Frank
Attendee: Lowe, Elizabeth
Pack/s: Basic conference pack
, Bonus conference pack

the two txt files are

confpack:
Basic conference pack
Bonus conference pack

and

employees:
Williams,Mary,Y
Nguyen,Vinh,Y
Kingsley,Margret
Kline,Bob,Y,Y
Mitchell,Frank,Y
Lowe,Elizabeth,Y,Y

something in my first two if statements is wrong but i'm at a loss.

i should mention the point of the code is to allocate the correct conference pack to the names. If they have 1 "Y" they get a conference pack, if theres 2 "Y" they get the bonus pack. My if statements must be incorrect because my code doesn't allocate the conference pack to people with a single "Y" .
Reply
#5
You're not stripping the newlines off the data. Without an rstrip() or similar, the final element of some of the names will be "Y\n", which doesn't match "Y".

That means you have lines where none of your cases are true (because the element is not "Y" and is not ""). But you never test for that, so you don't get any indication.
Reply
#6
(Aug-10-2020, 03:40 PM)bowlofred Wrote: You're not stripping the newlines off the data. Without an rstrip() or similar, the final element of some of the names will be "Y\n", which doesn't match "Y".

That means you have lines where none of your cases are true (because the element is not "Y" and is not ""). But you never test for that, so you don't get any indication.

Thanks for the help. I added in rstrip() but im getting the same result. Im quite new to programming, my mind is getting scrambled haha.

for line in employees.readlines():
Attendee = line.rstrip(" ")
Attendee = line.split(",") + ['','']
surname = Attendee[0]
firstName = Attendee[1]
print("Attendee: " + surname + ", " + firstName)
Reply
#7
Take a look at the documentation for rstrip(). If you don't pass in any arguments, it will remove trailing whitespace. But if you do pass them in, it will only remove those characters.

Your problem is that you have trailing newlines. By telling rstrip() to remove spaces, it will ignore the newlines. You probably just want to remove the argument completely.

>>> "joe,Y\n".rstrip()
'joe,Y'
>>> "joe,Y\n".rstrip(" ")
'joe,Y\n'
Reply
#8
(Aug-10-2020, 11:51 PM)bowlofred Wrote: Take a look at the documentation for rstrip(). If you don't pass in any arguments, it will remove trailing whitespace. But if you do pass them in, it will only remove those characters.

Your problem is that you have trailing newlines. By telling rstrip() to remove spaces, it will ignore the newlines. You probably just want to remove the argument completely.

>>> "joe,Y\n".rstrip()
'joe,Y'
>>> "joe,Y\n".rstrip(" ")
'joe,Y\n'

So i have removed the arguments for .rstrip() and tried .strip(), but my code still doesn't count the attendees with one "Y". sorry to be a pain in the butt.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error: audioio has no attribute 'AudioOut' netwrok 3 631 Oct-22-2023, 05:53 PM
Last Post: netwrok
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,337 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 5,250 Aug-17-2021, 05:24 PM
Last Post: Franky77
  Attribute Error received not understood (Please Help) crocolicious 5 2,673 Jun-19-2021, 08:45 PM
Last Post: crocolicious
  error in scapy attribute 'haslayer' evilcode1 5 6,517 Mar-02-2021, 11:19 AM
Last Post: evilcode1
  Stumped by my own code (ratio & epoch-time calculation). MvGulik 2 2,126 Dec-30-2020, 12:04 AM
Last Post: MvGulik
  Really stumped now Milfredo 7 3,067 Sep-17-2020, 06:18 AM
Last Post: Milfredo
  Still Stumped Milfredo 2 2,092 Sep-02-2020, 07:48 AM
Last Post: Milfredo
  attribute error instead of correct output MaartenRo 2 2,185 Aug-28-2020, 10:22 AM
Last Post: Larz60+
  Attribute Error - trying to create a pixel array out of PNG files The_Sarco 1 2,006 Apr-29-2020, 07:10 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020