微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python-从File.readlines返回列表中存在的给定文件列表的最高Int值

如何解决Python-从File.readlines返回列表中存在的给定文件列表的最高Int值

一个复杂的问题,我正在制作一个抽搐机器人,该机器人每隔X分钟就会获得一次聊天,并将其显示在小部件上。每当有人在聊天室中写一些东西时,都会在文件中记录他们发了多少条消息。我的事情是我需要它检查活动用户的抽搐返回的列表,查看哪些拥有文件,比较文件值,并返回具有最高值的文件名。

这是我到目前为止的代码(有效):

from twitchio.ext import commands
import json
from datetime import datetime
import os


new = {}
orig = {}
cAuthor = ""

def listToString(s):  
    
    # initialize an empty string 
    str1 = ""  
    
    # traverse in the string   
    for ele in s:  
        str1 += ele   
    
    # return string   
    return str1

def appendFile():
    inttochange = 0
    global cAuthor
    if os.path.exists(cAuthor) == True:
        with open(cAuthor,"r") as f:
            # Read lines and remove newline at the end of each line
            lines = [l.strip() for l in f.readlines()]
        l = int(listToString(lines))
    else:
        l = 0
    with open(cAuthor,"w+") as f:
        l += 1
        inttochange = str(l)
        f.write(str(l))
        f.close
        
class Bot(commands.Bot):

    def __init__(self):
        super().__init__(irc_token='token',nick='twitchbot',prefix='!',initial_channels=['#almostsomebody'])

    # Events don't need decorators when subclassed
    async def event_ready(self):
        print(f'Ready | {self.nick}')

    async def event_message(self,message):
        Now = datetime.Now() # current date and time
        time = Now.strftime("%H:%M:%s")
        print(time,message.author.name,"-",message.content)
        global cAuthor
        cAuthor = message.author.name
        appendFile()  

bot = Bot()
bot.run()

编辑:我已经找到了解决此问题的方法,并且它可以工作。这是将来可能遇到此问题的任何人的解决方案:

import os
import sys

users = []

def listToString(s):  
    
    # initialize an empty string 
    str1 = ""  
    
    # traverse in the string   
    for ele in s:  
        str1 += ele   
    
    # return string   
    return str1

def main():
    global users
    activeUsersExample = ["almostsomebody","example1","example2"]

    for item in activeUsersExample:
        print(item)
        with open(item) as f:
            for line in f:
                int_list = [int(i) for i in line.split()]
            print(int_list)
            count = int(listToString(line))
            line.strip("")
        users.append([item,count])
        print(users)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。