Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tuple no attribute error
#1
Linux Mint 18.2 Python 3.5. Am old time BASIC person.
Help. Have no understanding how to deal with this.
An example please.


 #!/usr/bin/python3
 #
 with open( 'expenses-18', 'r') as yrs_file:
     for line in enumerate(yrs_file):
         (pdate,catg,amt,howp) = line.split(',')
         pdate = str(pdate)
         length = len(pdate)
         catg = int(catg)
         amt = float(amt)
         howp = int(howp)
# Will do stuff here. 
         print pdate, catg, amt, howp

Traceback (most recent call last):
  File "try-me.py", line 5, in <module>
    (pdate,catg,amt,howp) = line.split(',')
AttributeError: 'tuple' object has no attribute 'split'
Reply
#2
The problem is enumerate. Enumerate returns a tuple of an index of the item and the item. Since you don't seem to be using an index for the lines in the loop, I would just loop over yrs_file, not enumerate(yrs_file).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
enumerate() returns a tuple containing two items. First is an index (starting with 0) and second is an item from the iterable you enumerated.

for line in enumerate(yrs_file):
Here line is a tuple (index, item), so you can't use split on it, which is a method of string type.
You could instead unpack the tuple into two variables like this:
for index, line in enumerate(yrs_file):
Then line will be a string which you can split.
You could also access only the second element of the tuple - the string you want to split:
for line in enumerate(yrs_file):
    (pdate,catg,amt,howp) = line[1].split(',')
But in any case, I don't really see why you would need enumerate() in your code at all.
Reply
#4
(Oct-06-2018, 02:51 PM)oldcity Wrote: Linux Mint 18.2 Python 3.5.
You can remove enumerate() as mention bye @j.crater.
I guess you have file that has lines with 4 values.
>>> line = 'a, 1, 2, 3'
>>> (pdate,catg,amt,howp) = line.split(',')
>>> pdate
'a'
>>> catg
' 1'
Quote:# Will do stuff here.
print pdate, catg, amt, howp
You are not using Python 3.5,this will give SyntaxError.
Have to use python3 to use Python 3.5 on mint 18.
print() is a function python 3.
print(pdate, catg, amt, howp)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error: audioio has no attribute 'AudioOut' netwrok 3 592 Oct-22-2023, 05:53 PM
Last Post: netwrok
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,210 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 5,164 Aug-17-2021, 05:24 PM
Last Post: Franky77
  Attribute Error received not understood (Please Help) crocolicious 5 2,611 Jun-19-2021, 08:45 PM
Last Post: crocolicious
  AttributeError: 'tuple' object has no attribute 'format' Anldra12 7 17,130 Apr-13-2021, 07:45 AM
Last Post: Anldra12
  error in scapy attribute 'haslayer' evilcode1 5 6,434 Mar-02-2021, 11:19 AM
Last Post: evilcode1
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,725 Nov-04-2020, 11:26 AM
Last Post: Aggam
  attribute error instead of correct output MaartenRo 2 2,147 Aug-28-2020, 10:22 AM
Last Post: Larz60+
  attribute error stumped on how to fix it. hank4eva 7 4,596 Aug-11-2020, 04:47 AM
Last Post: hank4eva
  Python Error- TypeError: ('Params must be in a list, tuple, or Row', 'HY000') DarkCoder2020 3 5,484 Jul-29-2020, 12:02 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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