首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python中的问题拆分

Python中的问题拆分
EN

Stack Overflow用户
提问于 2016-02-25 01:57:19
回答 1查看 74关注 0票数 1

目标是获取'%‘之后和'--’之前的文本。

然而,有时文本会在“--”之前被截断,比如在一个随机的字母处。为什么这不能正常工作?提前谢谢你!PS使用Python 3.5。

代码语言:javascript
复制
if destiny=='fortune':
    fortuneformat= fortune_print.split('%')[-1]
     print (fortuneformat)
    _1_fortuneformat= fortuneformat.split("--")[0]

    fortunesay = str(_1_fortuneformat)

仅供参考:

代码语言:javascript
复制
fortune_print= get_random_fortune('quotes1.txt')

这是用来获得财富的函数。

代码语言:javascript
复制
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

来自提供输入的文本文件的财富的示例输入:

代码语言:javascript
复制
%
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/ )

预期输出:The NSA knows what you did last summer. But no one, in the NSA or outside it, knows why they should.

实际输出:The NSA knows what yo

有人问我这个腌过的文件是怎么做的。这是在此函数中使用pickle.dump完成的:

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2016-02-25 02:42:56

为什么不使用正则表达式来寻找财富呢?

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35609553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档