9 Replies - 16208 Views - Last Post: 30 May 2011 - 12:50 PM Rate Topic: -----

#1 tootypegs  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 239
  • Joined: 09-October 07

basic string problems

Posted 30 May 2011 - 08:23 AM

sorry again guys, i feel like im on a posting frenzy! ive been stuck on thi all morning aswel and it looks like only basic stuff! my code all works well so far and prints out the string at the bottom of the post. However no matter what i do i cant fins a way to search that sstring for the word 'hello'!!! i cnt seem to plit it into a string like i did previously with your help. all i get it : AttributeError: 'list' object has no attribute 'split'

im stuck

 for filename in filenames:
        pathstring =  os.path.join(dirname, filename)
        f=open(pathstring, 'r')
        for lines in f.readlines():
          word = lines.split(" ")
        for word in lines.split(" "):
          tester = tagger(word)
          print  tester



printed output from string

[['hello', 'UH', 'hello']]


Is This A Good Question/Topic? 0
  • +

Replies To: basic string problems

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: basic string problems

Posted 30 May 2011 - 08:44 AM

This is a standard example of improper scoping. Calling split() on a string will create a list. Performing for...in on a list will perform the following (indented) actions on each item in that list.

From line 6 and onward, you are not indented properly. It will really help you understand the problem if you label your variables so that they represent what is actually stored in them. For instance, in your code you have lines and word; however lines is only a single line, whereas word is a list of words!

What I am sure you are looking for is this:
for filename in filenames:
  pathstring =  os.path.join(dirname, filename)
  f=open(pathstring, 'r')
  for line in f.readlines():
    words = line.split(" ")
    for word in words:
      tester = tagger(word)
      print  tester


Was This Post Helpful? 2
  • +
  • -

#3 tootypegs  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 239
  • Joined: 09-October 07

Re: basic string problems

Posted 30 May 2011 - 09:09 AM

ah, yer the indenting is taking ome getting used to, im till getting my head round it as its screaming at me all the time haha

Im not ure what you mean by the first bit. Im trying to turn the whole of 'tagger' from a list to a whole string so i can use 'find' on it.

Am i trying to split it into list still as it already is one? im confused, sorry
Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: basic string problems

Posted 30 May 2011 - 09:12 AM

View Posttootypegs, on 30 May 2011 - 12:09 PM, said:

Am i trying to split it into list still as it already is one?


Yes, you were trying to split a list. Hence: AttributeError: 'list' object has no attribute 'split'
Was This Post Helpful? 0
  • +
  • -

#5 tootypegs  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 239
  • Joined: 09-October 07

Re: basic string problems

Posted 30 May 2011 - 09:28 AM

right, i understand, so when i prints out in list form it looks like [['hello', 'UH', 'hello']]
i want to make that into a string so i can use find.

Ive looked into methods for converting list to string and they all said use split(). this is why im getting that error. is there not a straight forward convert list to string function?


thanks again
Was This Post Helpful? 0
  • +
  • -

#6 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: basic string problems

Posted 30 May 2011 - 09:44 AM

No, split turns a string into a list. You'll need to use join to turn a list into a string.

Did you try my code? Did it work?

This post has been edited by Motoma: 30 May 2011 - 09:45 AM

Was This Post Helpful? 0
  • +
  • -

#7 tootypegs  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 239
  • Joined: 09-October 07

Re: basic string problems

Posted 30 May 2011 - 10:36 AM

yer, it worked really, well. Right so its join that i have to implement. Il try it now and see. thanks

tried

tester.join(" ")


got an error AttributeError: 'list' object has no attribute 'join'. but surely list does?
Was This Post Helpful? 0
  • +
  • -

#8 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: basic string problems

Posted 30 May 2011 - 10:51 AM

join() is a member function of a string, not a list:

>>> mylist = ['hello', 'world']
>>> ''.join(mylist)
'helloworld'
>>> ' '.join(mylist)
'hello world'
>>> '-'.join(mylist)
'hello-world'


Was This Post Helpful? 1
  • +
  • -

#9 tootypegs  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 239
  • Joined: 09-October 07

Re: basic string problems

Posted 30 May 2011 - 11:02 AM

well i thought i understood but i mustnt

as tagger gives output in this form [['with', 'IN', 'with']] its a list

therefore
'-'.join(mylist)

should give me with-IN-with


but it doesnt, i get the error TypeError: sequence item 0: expected string, list found

do tagger not have the correct output for a list? it has a double ']' at each side is that whats causing issues?
Was This Post Helpful? 0
  • +
  • -

#10 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: basic string problems

Posted 30 May 2011 - 12:50 PM

Correct, what you actually have is list in a list holding a string. Since there's only one list inside the bigger list, this should get the job done:

'-'.join(mylist[0])

This post has been edited by atraub: 30 May 2011 - 03:02 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1