Split function to split sentence into words

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fellya
    New Member
    • Nov 2008
    • 11

    #16
    thank you for ths answer but that is not want i want,
    i have a file called ex1.py then to open it i do:
    Code:
    f=open("ex1.py")
    try:
    ........................
    then after all the procedures of opening a file i have:

    jack is a brother of carine, ............... ............... ............... ........


    My question is: is there anyway after opening this file which contain like 5 paragraph, to be splited into words?
    Last edited by numberwhun; Dec 9 '08, 05:52 PM. Reason: Add code tags!

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #17
      It seems that we already covered this:
      [code=Python]import re
      s = open('your_file ').read()
      wordList = []
      for sentence in [item.strip() for item in re.split(r'[!?.]', s.replace('\n', ' ')) if item]:
      wordList.append (sentence.split ())[/code]

      Comment

      • fellya
        New Member
        • Nov 2008
        • 11

        #18
        ohhhh thank you but using the codes i couldn't get anything. maybe i used it wrong. what do u mean when u put [! ? .] or ' '
        it seems like i was supposed to put something instead of those symbols.
        please look at what i did in the below code. ntbs1.py is my file.
        Code:
        >>> import re
        >>> s=open('ntbs1.py').read()
        >>> wordList=[]
        >>> for sentence in [item.strip() for item in re.split(r'[!?.]', s.replace('\n',
        '')) if item]:
        ...       wordList.append(sentence.split())
        ...
        >>>
        Last edited by numberwhun; Dec 9 '08, 05:51 PM. Reason: Add Code Tags!!!

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #19
          The sentences are split on the characters inside the brackets (!?.). Each newline character (\n) is replaced with a space character.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32633

            #20
            Fellya,

            You have been asked to enclose all your code within [ CODE ] tags. We have rules on this site that demand you do that. Please pay attention in future to making sure all your code is posted that way to ensure it is easier to understand and doesn't waste the time of our experts trying to decipher it.

            -Administrator.

            Comment

            Working...