python - 将文本列表分配给变量 python
问题描述
我有一个 python 脚本,它允许我检查电报中是否使用了一个数字。
我尝试将变量“phone_number”更改为一个基本上包含电话号码的 .txt 列表(每行有一个电话号码)我希望脚本从 file.txt 中获取一个电话号码检查它是否存在然后移动到下一个,依此类推,直到检查完所有数字。
这是我到目前为止所尝试的......
import random
from telethon import TelegramClient
from telethon import functions, types
import ast
api_id = XXXXX
api_hash = 'XXXXXXXXXXXXXXXXXX'
client = TelegramClient('session', api_id, api_hash)
async def main():
phone_in = []
with open('file.txt', 'r') as f:
phone_str = f.readline()
phone_in.append(ast.literal_eval(phone_str))
result = await client(functions.contacts.ImportContactsRequest(
contacts=[types.InputPhoneContact(
client_id=random.randrange(-2**63, 2**63),
phone=phone_in,
first_name='Some Name',
last_name=''
)]
))
if len(result.users):
print(f"{phone_in} has a telegram account")
await client(functions.contacts.DeleteContactsRequest(result.users))
else:
print(f"couldn't find an account for {phone_in}")
client.start()
client.loop.run_until_complete(main())
我试过这个,但我有一个错误,如下所示:
Traceback (most recent call last):
File "/Users/me/phone.py", line 33, in <module>
client.loop.run_until_complete(main())
File "/usr/local/Cellar/python@3.9/3.9.1_7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
return future.result()
File "/Users/me/phone.py", line 17, in main
result = await client(functions.contacts.ImportContactsRequest(
File "/usr/local/lib/python3.9/site-packages/telethon/client/users.py", line 30, in __call__
return await self._call(self._sender, request, ordered=ordered)
File "/usr/local/lib/python3.9/site-packages/telethon/client/users.py", line 58, in _call
future = sender.send(request, ordered=ordered)
File "/usr/local/lib/python3.9/site-packages/telethon/network/mtprotosender.py", line 174, in send
state = RequestState(request)
File "/usr/local/lib/python3.9/site-packages/telethon/network/requeststate.py", line 17, in __init__
self.data = bytes(request)
File "/usr/local/lib/python3.9/site-packages/telethon/tl/tlobject.py", line 194, in __bytes__
return self._bytes()
File "/usr/local/lib/python3.9/site-packages/telethon/tl/functions/contacts.py", line 498, in _bytes
b'\x15\xc4\xb5\x1c',struct.pack('<i', len(self.contacts)),b''.join(x._bytes() for x in self.contacts),
File "/usr/local/lib/python3.9/site-packages/telethon/tl/functions/contacts.py", line 498, in <genexpr>
b'\x15\xc4\xb5\x1c',struct.pack('<i', len(self.contacts)),b''.join(x._bytes() for x in self.contacts),
File "/usr/local/lib/python3.9/site-packages/telethon/tl/types/__init__.py", line 9789, in _bytes
self.serialize_bytes(self.phone),
File "/usr/local/lib/python3.9/site-packages/telethon/tl/tlobject.py", line 112, in serialize_bytes
raise TypeError(
TypeError: bytes or str expected, not <class 'list'>
这是相同的代码,但要检查的电话号码是“硬编码”的
import random
from telethon import TelegramClient
from telethon import functions, types
api_id = XXXXXXX
api_hash = 'XXXXXXXXXXXXXXXXX'
client = TelegramClient('session', api_id, api_hash)
async def main():
phone_number = '+XXXXXXXXX'
result = await client(functions.contacts.ImportContactsRequest(
contacts=[types.InputPhoneContact(
client_id=random.randrange(-2**63, 2**63),
phone=phone_number,
first_name='Some Name',
last_name=''
)]
))
if len(result.users):
print(f"{phone_number} has a telegram account")
await client(functions.contacts.DeleteContactsRequest(result.users))
else:
print(f"couldn't find an account for {phone_number}")
client.start()
client.loop.run_until_complete(main())
有谁知道我如何将 file.txt 分配给 phone_in 变量?
解决方案
如果 ImportContactsRequests 一次需要一个电话号码,那么您必须为每个电话号码调用它。这将为单个名称创建多个记录,但如果 API 不允许每个人使用多个电话号码,则您必须决定如何处理它。
with open('file.txt', 'r') as f:
phone_str = f.readline()
result = await client(functions.contacts.ImportContactsRequest(
contacts=[types.InputPhoneContact(
client_id=random.randrange(-2**63, 2**63),
phone=phone_str,
first_name='Some Name',
last_name=''
)]
))
if len(result.users):
print(f"{phone_number} has a telegram account")
await client(functions.contacts.DeleteContactsRequest(result.users))
else:
print(f"couldn't find an account for {phone_number}")
推荐阅读
- c# - Monogame - 第二个脚本/类中的级别
- android - 以编程方式聚焦编辑文本 (Kotlin)
- javascript - Node Js Firestore 使用空格访问数据库值
- python - 使用 Python Pandas 检查项目是否以前出现在项目列表中
- php - Wordpress 帖子使用特定类别中的命令标签计数
- python - 处理具有深度信息的图像
- angular - Angular 4 中的 @Input @Output 和 EventEmitter
- vba - Excel 2016 宏无法找到文件:运行时错误 1004
- c# - 如何使用另一个单选按钮替换已在富文本框中设置的文本?
- average - Power BI:求平均值的平均值和平均值的 STDEV.P