python script error

19 views
Skip to first unread message

Ekkam Singh

unread,
Mar 19, 2021, 8:49:58 AM3/19/21
to BeagleBoard
hi 
i am writing a code in python but it is coming error when i write the program :-
for line in username:
                content = username.split(' , ')
                if content[0] == userName:
                                content[1] = score
                                line = content[0] + ', ' + content[1] + '\n'
it is coming error :
AttributeError: '_io.TextIOWrapper' object has no attribute 'split'

jonnymo

unread,
Mar 19, 2021, 11:41:59 AM3/19/21
to Beagle Board
What version of Python are you using? 
How is 'username' defined and how is the value being assigned to it?

Cheers,

Jon 

--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/35de0395-e66c-41e4-b574-0a52dd6d7bc8n%40googlegroups.com.

Dennis Lee Bieber

unread,
Mar 19, 2021, 6:12:24 PM3/19/21
to Beagleboard
On Thu, 18 Mar 2021 23:42:04 -0700 (PDT), in
gmane.comp.hardware.beagleboard.user Ekkam Singh
<ekkhanuja-Re5JQE...@public.gmane.org> wrote:

>hi
>i am writing a code in python but it is coming error when i write the
>program :-
>for line in username:
> content = username.split(' , ')

content = line.split(",")

NOTE: your split is explicitly expecting <space><comma><space> which is
an unnatural usage of commas. Better, I would think, is to use
content[*].strip() to get rid of the spaces after splitting on the comma.

> if content[0] == userName:
> content[1] = score
> line = content[0] + ', ' + content[1] + '\n'

Confusing usage. Your main input is a list or iterable called
"username", yet you are expecting the first term of each LINE of that
iterable to contain something you compare to whatever userName contains.

Also, note that you are appending a newline when recreating line, but
does the unmodified line end with a newline? (I'm assuming you later write
either the modified or unmodified line to some file).

There is no need to bind "score" to content[1] if the only usage is
then to join strings. Also, "score" needs to be a string for that joining
to work -- if it is numeric you need to convert it. Consider:

for line in username: #still don't like that object name...
#user_scores may be more applicable.
uName = line.split(",")[0].strip() #don't care about rest
if uName == userName:
line = "%s, %s\n" % (uName, score)
#using %s means whatever "score" type, it will
#be converted to a string representation




--
Dennis L Bieber

Reply all
Reply to author
Forward
0 new messages