cut_text() 函数在 bilibili 链接解析 cut 简介时概率触发:报错 index out of range,尝试修复(见 复现及修复 部分)后结果不美观
报错信息
Line 131:
if p[-1] == "\n":
IndexError: string index out of range
复现及修复
import string
# 源于 utils/text_util.py
def cut_text(str, cut):
"""
:说明: `get_cut_str`
> 长文本自动换行切分
:参数:
* `str: [type]`: 文本
* `cut: [type]`: 换行宽度,需要至少大于20
:返回:
- `List`: 切分后的文本列表
"""
punc = """,,、。.??)》】“"‘';;::!!·`~%^& """
si = 0
i = 0
next_str = str
str_list = []
for s in next_str:
if s in string.printable:
si += 1
else:
si += 2
i += 1
if next_str[0] == "\n":
next_str = next_str[1:]
elif s == "\n":
str_list.append(next_str[: i - 1])
next_str = next_str[i - 1 :]
si = 0
i = 0
continue
if si > cut:
try:
if next_str[i] in punc:
i += 1
if next_str[i] in string.ascii_letters:
for j in range(i, i - 18, -1):
if next_str[j] == " ":
i = j + 1
break
except IndexError:
str_list.append(next_str)
return str_list
str_list.append(next_str[:i])
next_str = next_str[i:]
si = 0
i = 0
str_list.append(next_str)
i = 0
non_wrap_str = []
for p in str_list:
# FIXME 原文件: index out of range
# ----------------------------
# if p[-1] == "\n":
# p = p[:-1]
# ----------------------------
# 修改后: 不会 index out of range 但效果不美观
# ----------------------------
if p.endswith("\n"):
p = p[:-1]
# ----------------------------
non_wrap_str.append(p)
i += 1
return non_wrap_str
# 出问题的字符串
src = """https://youtu.be/m_RqjvVvvfA
💗🎃💗※イヤホンで聴くと左右動いて、とっても楽しくてオススメです。💗🎃💗
「ヒトよ、一夜に一目惚レ!?」
新曲完成しました。
ゾクゾクしたいそんな夜にどうぞ…
テーマは『信じてる』
イラストは田ゃむさん。イラスト最高なのでフォローするように。
もしよかったらコメント、チャンネル登録♪
「なんねぇコレ!?」と思ったら低評価ボタン🥰お待ちしてます。
映像・音楽:みつあくま https://twitter.com/mitsu_akuma
イラスト:田ゃむ https://twitter.com/tanabe0607
https://twitter.com/tanabe0607/status...
終わりペンギン:もずくず https://twitter.com/mzkzmm
プロセカNEXT応募曲です。ニーゴ好きでスマンな🥺
第9回プロセカNEXT応募楽曲
コミュニティも最近、お世辞抜きに賑わいつつ更新中😍(国籍言語問わず絡んでね)
https://www.youtube.com/channel/UCtne...
ジャンル:HYPER J-POP
#初音ミク #プロセカNEXT #みつあくま
サブテーマは処女懐胎 (Virgin birth)
Hey, rumors I heard recently
I feel so sad that I feel like I'm going to vomit
Day By Day, Mirror with bloody cracks
The girl was crouching and laughing at the front door
I put up with the pain
I'm used to the liquid with a bitter taste
I would be confused without you
Hey you? I will be a girl who is convenient for you...
Love...
Evidence destruction, girl innocence
Please keep it secret from mom
The bruise has disappeared, but the life (body) remains dirty
Are you ready? not yet
Crack in the heart It's just strange
Macaroons that are hard and cannot be chewed
Where should I hide it? I can't rely on anyone
Are you ready? that's enough
Common worries
Waste money Temporary illness
Give up and light a lighter
Hey, that child with the nickname "bye fungus"
Bye-bye, missing from Friday
The hair quality was terrible
Is it fate that both parents and children cry in the locker?
Prohibition of unexplained use
Irresponsible and precious warmth
There were a lot of talisman stuck in the locker
Hey, Don't be silly
(At first glance!?)
Evidence destruction, display of girl
Without knowing mom
Spinning from pleasure
Innocently frozen finger
Hey good?
It's okay today
Feelings are anemic, bleeding ^^
Junk Kagura Karma
Who should I go to?
I can't tell anyone
I'm done ...
that's enough"""
# 此行源于 plugins/bili_resolve.py (line 130 in `binfo_image_create()` function)
# dynamic_cut_str = "\n".join(cut_text(dynamic, 58))
# 问题复现
items = cut_text(src, 58)
for item in items:
# 文本 cut 后
print(item)
运行结果
https://youtu.be/m_RqjvVvvfA
💗🎃💗※イヤホンで聴くと左右動いて、とっても楽しくてオススメ
です。💗🎃💗
「ヒトよ、一夜に一目惚レ!?」
新曲完成しました。
ゾクゾクしたいそんな夜にどうぞ…
テーマは『信じてる』
イラストは田ゃむさん。イラスト最高なのでフォローするように。
もしよかったらコメント、チャンネル登録♪
「
なんねぇコレ!?」と思ったら低評価ボタン🥰お待ちしてます。
映
像・音楽:みつあくま https://twitter.com/mitsu_akuma
イラスト:田ゃむ https://twitter.com/tanabe0607
https://twitter.com/tanabe0607/status...
終わりペンギン:もずくず https://twitter.com/mzkzmm
プロセカNEXT応募曲です。ニーゴ好きでスマンな🥺
第
9回プロセカNEXT応募楽曲
コミュニティも最近、お世辞抜きに賑わいつつ更新中😍(国籍言語
問わず絡んでね)
https://www.youtube.com/channel/UCtne...
ジャンル:HYPER J-POP
#初音ミク #プロセカNEXT #みつあくま
サブテーマは処女懐胎 (Virgin birth)
Hey, rumors I heard recently
I
feel so sad that I feel like I'm going to vomit
Day By Day, Mirror with bloody cracks
The girl was crouching and laughing at the front door
I put up with the pain
I'm used to the liquid with a bitter taste
I would be confused without you
Hey you? I will be a girl who is convenient for you...
Love...
Evidence destruction, girl innocence
Please keep it secret from mom
The bruise has disappeared, but the life (body) remains
di
rty
Are you ready? not
yet
Crack in the heart It's just st
range
Macaroons that are hard and cannot be
chewed
Where should I hide it? I can't rely on
anyone
Are you ready? that'
s enough
Comm
on worries
Waste money Tempor
ary illness
Give up and lig
ht a lighter
Hey, that child with the nicknam
e "bye fungus"
Bye-bye, miss
ing from Friday
The hair qual
ity was terrible
Is it fate that both parents and children
c
ry in the locker?
Prohibition
of unexplained use
Irresponsible
and precious warmth
There were a lot of talisma
n stuck in the lock
er
Hey, Don't be si
lly
(At first glance!?)
Evidence de
struction, display
of girl
Without knowin
g mom
Spinning from plea
sure
Inno
cently frozen f
inger
Hey good?
It's okay today
Feelings are anem
ic, bleeding ^^
Jun
k Kagura Karma
Who
should I go
to?
I can't tell anyone
I'm done ...
that's enough
预期结果
https://youtu.be/m_RqjvVvvfA
💗🎃💗※イヤホンで聴くと左右動いて、
とっても楽しくてオススメです。💗🎃💗
「ヒトよ、一夜に一目惚レ!?」
新曲完成しました。
ゾクゾクしたいそんな夜にどうぞ…
テーマは『信じてる』
イラストは田ゃむさん。
イラスト最高なのでフォローするように。
もしよかったらコメント、チャンネル登録♪
「なんねぇコレ!?」と思ったら低評価ボタン🥰お待ちしてます。
映像・音楽:みつあくま
https://twitter.com/mitsu_akuma
イラスト:田ゃむ
https://twitter.com/tanabe0607
https://twitter.com/tanabe0607/status...
終わりペンギン:もずくず
https://twitter.com/mzkzmm
プロセカNEXT応募曲です。ニーゴ好きでスマンな🥺
第9回プロセカNEXT応募楽曲
コミュニティも最近、お世辞抜きに賑わいつつ更新中😍
(国籍言語問わず絡んでね)
https://www.youtube.com/channel/UCtne...
ジャンル:HYPER J-POP
#初音ミク #プロセカNEXT #みつあくま
サブテーマは処女懐胎 (Virgin birth)
Hey, rumors I heard recently
I feel so sad that I feel like
I'm going to vomit Day By Day,
Mirror with bloody cracks
The girl was crouching
and laughing at the front door
I put up with the pain
I'm used to the liquid with a bitter taste
I would be confused without you
Hey you?
I will be a girl who is convenient for you...
Love...
Evidence destruction, girl innocence
Please keep it secret from mom
The bruise has disappeared,
but the life (body) remains dirty
Are you ready? not yet
Crack in the heart It's just strange
Macaroons that are hard and cannot be chewed
Where should I hide it?
I can't rely on anyone
Are you ready? that's enough
Common worries
Waste money Temporary illness
Give up and light a lighter
Hey, that child with the nickname "bye fungus"
Bye-bye, missing from Friday
The hair quality was terrible
Is it fate that both parents
and children cry in the locker?
Prohibition of unexplained use
Irresponsible and precious warmth
There were a lot of talisman stuck in the locker
Hey, Don't be silly
(At first glance!?)
Evidence destruction, display of girl
Without knowing mom
Spinning from pleasure
Innocently frozen finger
Hey good?
It's okay today
Feelings are anemic, bleeding ^^
Junk Kagura Karma
Who should I go to?
I can't tell anyone
I'm done ...
that's enough
关于工具文件夹
utils
下text_util.py
中cut_text()
函数的 bug问题描述
cut_text()
函数在 bilibili 链接解析 cut 简介时概率触发:报错index out of range
,尝试修复(见复现及修复
部分)后结果不美观报错信息
复现及修复
运行结果
预期结果