Python Forum

Full Version: attribute error stumped on how to fix it.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
I think you're getting splitlines() and readlines() confused. There is a splitlines(), but it's a str method, not a file-type method.
(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!
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" .
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.
(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)
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'
(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.