Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔎 提供一个长字符限制的解决办法 #331

Open
StriveZs opened this issue May 7, 2023 · 2 comments
Open

🔎 提供一个长字符限制的解决办法 #331

StriveZs opened this issue May 7, 2023 · 2 comments

Comments

@StriveZs
Copy link

StriveZs commented May 7, 2023

这里以星座运势为例,考虑到现在长字符会被添加省略号,因此我尝试将长文本拆分为多个短文本来进行拼接处理,下面是我拆分文本的代码,有一些参数可以调整,比如maxLen来调整短文本的长度,maxList为最大设置的短文本的数量。
`

星座运势处理

s1 = horoscope['horoscope1']
s2 = horoscope['horoscope2']
r1 = []
r2 = []
maxLen = 19
maxList = 6
for i in range(len(s1)):
    if i % maxLen == 0 and i != 0:
        r1.append(s1[i-maxLen : i])
t = len(s1) % maxLen
r1.append(s1[len(s1)-t:])
if len(r1) < maxList:
    for i in range(maxList - len(r1)):
        r1.append('')
horoscope['horoscope1'] = r1

for i in range(len(s2)):
    if i % maxLen == 0 and i != 0:
        r2.append(s2[i-maxLen : i])
t = len(s2) % maxLen
r2.append(s2[len(s2)-t:])
if len(r2) < maxList:
    for i in range(maxList - len(r2)):
        r2.append('')
horoscope['horoscope2'] = r2
print(horoscope)

`

通过上述代码可以将长文本拆分成一个个短文本,同时如果你的文本字符数小于maxLen × maxList的总数,我这里考虑用''来补齐list的总数。然后只需要在传输json数据的时候将list中的每个元素分别传输即可,样例代码如下:
"horoscope1_0": { "value": horoscope['horoscope1'][0], "color": get_color() }, "horoscope1_1": { "value": horoscope['horoscope1'][1], "color": get_color() }, "horoscope1_2": { "value": horoscope['horoscope1'][2], "color": get_color() }, "horoscope1_3": { "value": horoscope['horoscope1'][3], "color": get_color() }, "horoscope1_4": { "value": horoscope['horoscope1'][4], "color": get_color() }, "horoscope1_5": { "value": horoscope['horoscope1'][5], "color": get_color() }, "horoscope2_0": { "value": horoscope['horoscope2'][0], "color": get_color() }, "horoscope2_1": { "value": horoscope['horoscope2'][1], "color": get_color() }, "horoscope2_2": { "value": horoscope['horoscope2'][2], "color": get_color() }, "horoscope2_3": { "value": horoscope['horoscope2'][3], "color": get_color() }, "horoscope2_4": { "value": horoscope['horoscope2'][4], "color": get_color() }, "horoscope2_5": { "value": horoscope['horoscope2'][5], "color": get_color() },

这样我们只需要在微信测试号模板中,将各个短文本拼接起来即可,如下:
今日运势1: {{horoscope1_0.DATA}} {{horoscope1_1.DATA}}{{horoscope1_2.DATA}}{{horoscope1_3.DATA}}{{horoscope1_4.DATA}}{{horoscope1_5.DATA}} 今日运势2: {{horoscope2_0.DATA}}{{horoscope2_1.DATA}}{{horoscope2_2.DATA}}{{horoscope2_3.DATA}}{{horoscope2_4.DATA}}{{horoscope2_5.DATA}}
这样就可以实现长文本的显示了。存在的问题的话就是当长文本超过maxLen × maxList的时候可能会无法显示超出本文的内容。

这里提供一个思路。

效果如下:
image

当然目前彩色字体还没办法解决,希望有大佬可以分享解决办法。

@StriveZs StriveZs changed the title 提供一个长字符限制的解决办法 🔎 提供一个长字符限制的解决办法 May 7, 2023
@shangzeng
Copy link

大佬能更详细一些么。。

@StriveZs
Copy link
Author

StriveZs commented Jun 14, 2023

大佬能更详细一些么。。

现在的问题是存在可显示字符的数量限制,一次可能只能显示大概19个字符?

因此考虑解决方案就是:我将长字符串拆分成多个短字符串(比如单个短字符串19个字符),然后将它们组成一个字典传输,然后再将这些内容拼接起来就好了。

具体使用方式就是将下面代码插入到你python获取星座运势或者其他长文本内容之后:
...... (代码省略) # 调用获取星座运势的函数 horoscope = get_horoscope(config) #print(note_ch) #print(note_en) print(horoscope) # 星座运势处理代码 s1 = horoscope['horoscope1'] s2 = horoscope['horoscope2'] r1 = [] r2 = [] maxLen = 19 maxList = 6 for i in range(len(s1)): if i % maxLen == 0 and i != 0: r1.append(s1[i-maxLen : i]) t = len(s1) % maxLen r1.append(s1[len(s1)-t:]) if len(r1) < maxList: for i in range(maxList - len(r1)): r1.append('') horoscope['horoscope1'] = r1 for i in range(len(s2)): if i % maxLen == 0 and i != 0: r2.append(s2[i-maxLen : i]) t = len(s2) % maxLen r2.append(s2[len(s2)-t:]) if len(r2) < maxList: for i in range(maxList - len(r2)): r2.append('') horoscope['horoscope2'] = r2 print(horoscope) # 公众号推送消息 for user in users: send_message(user, accessToken, city, weather, max_temperature, min_temperature, note_ch, note_en, horoscope) os.system("pause")

推送完消息之后,只需要在推送消息的函数中,将字典中每个元素都发送到公众号端即可:
data = { ... "note_ch": { "value": note_ch, "color": get_color() }, "horoscope1_0": { "value": horoscope['horoscope1'][0], "color": get_color() }, "horoscope1_1": { "value": horoscope['horoscope1'][1], "color": get_color() }, "horoscope1_2": { "value": horoscope['horoscope1'][2], "color": get_color() }, "horoscope1_3": { "value": horoscope['horoscope1'][3], "color": get_color() }, "horoscope1_4": { "value": horoscope['horoscope1'][4], "color": get_color() }, "horoscope1_5": { "value": horoscope['horoscope1'][5], "color": get_color() }, "horoscope2_0": { "value": horoscope['horoscope2'][0], "color": get_color() }, "horoscope2_1": { "value": horoscope['horoscope2'][1], "color": get_color() }, "horoscope2_2": { "value": horoscope['horoscope2'][2], "color": get_color() }, "horoscope2_3": { "value": horoscope['horoscope2'][3], "color": get_color() }, "horoscope2_4": { "value": horoscope['horoscope2'][4], "color": get_color() }, "horoscope2_5": { "value": horoscope['horoscope2'][5], "color": get_color() }, } } ....(代码省略) response = post(url, headers=headers, json=data).json() if response["errcode"] == 40037: print("推送消息失败,请检查模板id是否正确") elif response["errcode"] == 40036: print("推送消息失败,请检查模板id是否为空") elif response["errcode"] == 40003: print("推送消息失败,请检查微信号是否正确") elif response["errcode"] == 0: print("推送消息成功") else: print(response)

然后在公众号测试的将信息拼接起来就好了(格式和上面相同)

上述内容都是在python函数中进行了修改,可以简单看一下代码 很容易懂的,主要就是将获得的星座运势信息进行重新处理一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants