gpt4 book ai didi

python - Python 中的问题拆分

转载 作者:行者123 更新时间:2023-12-01 04:05:51 25 4
gpt4 key购买 nike

目标是获取“%”之后和“--”之前的文本。

但是有时文本会在“--”之前被切断,例如随机字母。为什么这不能正常工作?先感谢您! PS使用Python 3.5。

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

fortunesay = str(_1_fortuneformat)

仅供引用:

fortune_print= get_random_fortune('quotes1.txt')

这是用来发财的函数。

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

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

%
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/ )

预期输出:美国国家安全局知道你去年夏天做了什么。但无论是在国家安全局内部还是外部,没有人知道为什么他们应该这么做。

实际输出:美国国家安全局知道你在做什么

有人问我腌制文件是如何制作的。这是通过此函数中的 pickle.dump 完成的:

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()

最佳答案

为什么不直接使用正则表达式来算命呢?

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

关于python - Python 中的问题拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35609553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com