1

The goal is to get the text after '%' and before '--'.

However sometimes text is being cut off before '--', like at a random letter. Why is this not working properly? Thank you in advance! PS using Python 3.5.

if destiny=='fortune':
    fortuneformat= fortune_print.split('%')[-1]
     print (fortuneformat)
    _1_fortuneformat= fortuneformat.split("--")[0]

    fortunesay = str(_1_fortuneformat)

FYI:

fortune_print= get_random_fortune('quotes1.txt')

And here is the function used to get a fortune.

def get_random_fortune(fortune_file):
    """
    Get a random fortune from the specified file. Barfs if the corresponding
    ``.dat`` file isn't present.byt
    :Parameters:
        fortune_file : str
            path to file containing fortune cookies
    :rtype:  str
    :return: the random fortune
    """
    fortune_index_file = fortune_file + '.dat'
    if not os.path.exists(fortune_index_file):
        print( 'Can\'t find file "%s"' % fortune_index_file)

    fortuneIndex = open(fortune_index_file, 'rb')
    data = pickle.load(fortuneIndex)
    fortuneIndex.close()
    randomRecord = random_int(0, len(data) - 1)
    (start, length) = data[randomRecord]

    f = open(fortune_file, 'rU')
    f.seek(start)
    fortuneCookie = f.read(length)
    f.close()
    return fortuneCookie

Example input of a fortune from the text file that is providing input:

%
The NSA knows what you did last summer. But no one, in the NSA or outside it,
knows why they should.

    -- Shlomi Fish
    -- NSA Facts by Shlomi Fish and Friends ( http://www.shlomifish.org/humour/bits/facts/NSA/ )

Expected output: The NSA knows what you did last summer. But no one, in the NSA or outside it, knows why they should.

Actual output: The NSA knows what yo

I have been asked how the pickled file is made. It is done with pickle.dump in this function:

def make_fortune_data_file(fortune_file, quiet=False):
    """
    Create or update the data file for a fortune cookie file.
    :Parameters:
        fortune_file : str
            path to file containing fortune cookies
        quiet : bool
            If ``True``, don't display progress messages
    """
    fortune_index_file = fortune_file + '.dat'
    if not quiet:
        pass
        #print ('Updating "%s" from "%s"...' % (fortune_index_file, fortune_file))

    data = []
    shortest = sys.maxsize
    longest = 0
    for start, length, fortune in _read_fortunes(open(fortune_file, 'rU')):
        data += [(start, length)]
        shortest = min(shortest, length)
        longest = max(longest, length)

    fortuneIndex = open(fortune_index_file,'wb')
    pickle.dump(data, fortuneIndex,protocol=4,fix_imports=True)
    fortuneIndex.close()
5
  • can you posted sample sample input, and expected output vs actual output? Commented Feb 24, 2016 at 17:59
  • Code looks fine at a glance. Post a minimal reproducible example. Commented Feb 24, 2016 at 18:00
  • Hi I added the sample input, and expected output vs actual output. I hope this makes my post easier to analyze. Thank you so much. Commented Feb 24, 2016 at 18:25
  • Are you sure fortune_print contains what you think it does? How did you create the pickled file? Commented Feb 24, 2016 at 18:39
  • @Holloway I am new to pickle, just FYI. I have updated the code to include how the pickled file is made. See bottom of the original post. Thanks! Commented Feb 24, 2016 at 19:07

1 Answer 1

2

Why not just use regex to find the fortunes instead?

s = """%
The NSA knows what you did last summer. But no one, in the NSA or outside it,
knows why they should.

    -- Shlom%i Fish
    -- NSA Facts by Shlomi Fish and Friends 
( http://www.shlomifish.org/humour/bits/facts/NSA/ )
% XSLT is the worst thing since non-sliced bread. -- Shlomi Fish -- 
XSLT Facts by Shlomi Fish and Friends ("""

re.findall("(?s)(?<=%).*?(?=--)",s)


Out[154]:
['\nThe NSA knows what you did last summer. But no one, in the NSA or outside it,\nknows why they should.\n\n    ',
 'i Fish\n    ',
 ' XSLT is the worst thing since non-sliced bread. ']

https://regex101.com/r/dQ9bD3/1

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! This works for some but not all. For instance - original fortune text: % XSLT is the worst thing since non-sliced bread. -- Shlomi Fish -- XSLT Facts by Shlomi Fish and Friends ( shlomifish.org/humour/bits/facts/XSLT ) % Output from regex: []
Output before regex applied (by using print (fortune_print)): mmer Glau Facts by Shlomi Fish and Friends ( shlomifish.org/humour/bits/facts/Summer-Glau ) % XSLT is the worst thing since non-sliced bread. -- Shlomi Fish -- X
@stuffatwork190 Please do not add code or line-separated output via comments. As you can see, the formatting does not make sense.
Regex is updated - should work for all examples now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.