csv 需要一些解决方案“bytes or str expected error”[关闭]

7kjnsjlb  于 2023-04-03  发布在  其他
关注(0)|答案(2)|浏览(101)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
昨天关门了。
Improve this question
有一个问题我解决不了。我是一个学生,不知道很多解决方法
这是我的代码:

from telethon.sync import TelegramClient
from telethon.sessions import StringSession
from telethon import functions, types
from keep_alive import keep_alive
import asyncio
import os
import time
import fileinput
import random
import csv
from telethon import errors
from telethon.sync import TelegramClient

api_id = input("API ID: ")
api_hash = input("API HASH: ")
correct_channel = input("CORRECT CHANNEL: ")

ses_string = os.getenv('ses_string')
    
keep_alive()

starting_points = [random.randint(0, 5) for i in range(3)]
read_nbr_of_lines = 2

for sp in starting_points:
    print('random starting line: %s'%sp)
    read_lines = 0
    with open('username.csv') as cf:
        lines = csv.reader(cf)
        for nbr, line in enumerate(lines):
            if nbr < sp - 1: continue
            read_lines += 1
            if read_lines > read_nbr_of_lines: break
            print(nbr, line)
          
print('A randomly selected username is:', lines)

with TelegramClient(StringSession(ses_string), api_id, api_hash) as client:
    while True:
        try:
            time.sleep(10)
            result = client(functions.channels.UpdateUsernameRequest(
               channel= correct_channel ,
               username = starting_points
            ))
        except Exception as e:
            print(type(e), str(e))

我希望代码从我的CSV文件中随机或以运行和重复的顺序选择一行并将其插入username = ???????????

vktxenjb

vktxenjb1#

我不确定我是否完全理解你的问题,但我建议使用itertools进行简化:

with open('username.csv') as f:
    reader = csv.reader(f)
    # substitute your values
    starting_points = [random.randint(0, 5) for i in range(3)] 
    for sp in starting_points:
        print('random starting line:', sp)
        skipped_lines = itertools.islice(reader, sp - 1)
        next(skipped_lines, None)
        line = next(reader)
        print(line)

干杯:)

mwkjh3gx

mwkjh3gx2#

试试这个:

from telethon.sync import TelegramClient
from telethon.sessions import StringSession
from telethon import functions, types
from keep_alive import keep_alive
import asyncio
import os
import time
import fileinput
import random
import csv
from telethon import errors
from telethon.sync import TelegramClient

api_id = input("API ID: ")
api_hash = input("API HASH: ")
correct_channel = input("CORRECT CHANNEL: ")

ses_string = os.getenv('ses_string')
keep_alive()

starting_points = [random.randint(0, 5) for i in range(3)]
read_nbr_of_lines = 2

for sp in starting_points:
    print('random starting line: %s'%sp)
    read_lines = 0
    with open('username.csv') as cf:
        lines = csv.reader(cf)
        for nbr, line in enumerate(lines):
            if nbr < sp - 1: continue
            read_lines += 1
            if read_lines > read_nbr_of_lines: break
            print(nbr, line)

random_user = line[0]
print('A randomly selected username is:', random_user)

with TelegramClient(StringSession(ses_string), api_id, api_hash) as client:
    while True:
        try:
            time.sleep(10)
            result = client(functions.channels.UpdateUsernameRequest(
               channel= correct_channel ,
               username = random_user
            ))
        except Exception as e:
            print(type(e), str(e))

因为line是一个列表,所以你可以使用line[0]来获取第一个也是唯一一个字符串,或者将其赋值给一个新的变量random_user并在代码中使用它。

相关问题