How to fix 'int' object has no attribute 'astype' error when sending WhatsApp messages to large number of contacts using Python and pandas?

huangapple go评论59阅读模式
英文:

How to fix 'int' object has no attribute 'astype' error when sending WhatsApp messages to large number of contacts using Python and pandas?

问题

错误信息显示了一个属性错误,指出在代码的第9行,尝试将一个整数转换为字符串时出现了问题。这可能是因为数据表中的某些值是整数而不是字符串。要解决这个问题,您可以使用str()函数将整数强制转换为字符串,如下所示:

cellphone = str(data.loc[i, 'Cellphone'])

这样,无论数据表中的值是整数还是字符串,都会被正确地处理。这应该有助于您在处理1700+个号码时避免出现这个错误。

英文:
AttributeError: 'int' object has no attribute 'astype' in automatic WhatsApp message sender script

The following is an automated WhatsApp message sender script I partially developed. I tried the following script and it worked fine with an excel with 5 numbers in it. However, I tried upscaling it to 1700+ numbers, and I get the following traceback:

Traceback (most recent call last):
  File "c:\Users\MSI\Desktop\AutoSenderPY\main.py", line 9, in <module>
    cellphone = data.loc[i,'Cellphone'].astype(str)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'astype'*

The script is the following:

import pandas as pd
import webbrowser as web
import pyautogui as pg
import time

data = pd.read_excel("book1.xlsx", sheet_name='sheet1')

for i in range(len(data)):
    cellphone = data.loc[i,'Cellphone'].astype(str) 
    
    message = "Test Message"
    
    web.open("https://web.whatsapp.com/send?phone=" + cellphone + "&text=" + message)
    
    time.sleep(5.5)           
    pg.click(1230,964)      
    time.sleep(1)            
    pg.press('enter')       
    time.sleep(2)         
    pg.hotkey('ctrl', 'w') 
    time.sleep(1)

Why is that happening, and how can I get it working for those 1700+ numbers?

答案1

得分: 1

尝试使用 -

cellphone = str(data.loc[i,'Cellphone'])

我认为loc返回一个类型为"numpy.int64"的单个元素,调用"str"应该足够。

英文:

Try using -

cellphone = str(data.loc[i,'Cellphone'])

I think loc returns a single element of type "numpy.int64", calling the "str" should be enough.

huangapple
  • 本文由 发表于 2023年6月1日 12:18:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定