簡體   English   中英

列表索引必須是整數或切片,而不是 str 為什么會出錯

[英]List indices must be integers or slices, not str why the error

我試圖為電話號碼驗證器集成 LoqateAPI,我想在 txt 文件中驗證批量電話號碼

波紋管是我的代碼

有人在這里幫助我嗎,我沒想到會出現這種錯誤

phone_number = (
    open(
        input(f"\n{bwh}{ble}Enter Phone Number List{res}{gr} [phone.txt]{res} : "), "r"
    )
    .read()
    .splitlines()
)
access_key = input(
    f"\n{bwh}{ble}Enter Your LoqateAPI Key {res}{gr} [AA11-AA11-AA11-AA11]{res} : "
)
country_code = input(f"\n{bwh}{ble}Enter Country Code {res}{gr} [US]{res} : ")
print("------------------------------------------------------------------")
for i in phone_number:
    url = (
        "https://api.addressy.com/PhoneNumberValidation/Interactive/Validate/v2.20/json.ws?&Key="
        + access_key
        + "&Phone="
        + str(i)
        + "&Country="
        + country_code
    )
    response = requests.get(url)
    answer = response.json()
    if answer["carrier"]:

        print(url)
        print(f'{gr}{answer["number"]}{res}{yl} => {cy}{answer["carrier"]}{res}')
        save = open(f'Result/{answer["carrier"]}.txt', "a+")
        save.write(str(i) + "\n")
    else:
        print(f'{red}{answer["number"]} => Die{res}')
        dk = open("Result/die.txt", "a+")
        dk.write(str(i) + "\n")


input("Click Enter For Exit ...!")

下面的文字是我的 output下面是我遇到的問題

錯誤圖片

**

編輯我從字符串中打印出響應

**

但響應是在 json 中,這是什么類型的 json 請我想知道

結果

    [{'PhoneNumber': '+14143392867', 'RequestProcessed': 'True', 'IsValid': 'Yes', 'NetworkCode': '180', 'NetworkName': 'AT&T Wireless', 'NetworkCountry': 'US', 'NationalFormat': '(414) 339-2867', 'CountryPrefix': '1', 'NumberType': 'Mobile'}]
[{'PhoneNumber': '+14146789198', 'RequestProcessed': 'True', 'IsValid': 'Yes', 'NetworkCode': '910', 'NetworkName': 'Google (Grand Central) - SVR', 'NetworkCountry': 'US', 'NationalFormat': '(414) 678-9198', 'CountryPrefix': '1', 'NumberType': 'VOIP'}]
[{'PhoneNumber': '+14148458738', 'RequestProcessed': 'False', 'IsValid': 'Maybe', 'NetworkCode': '', 'NetworkName': '', 'NetworkCountry': 'US', 'NationalFormat': '(414) 845-8738', 'CountryPrefix': '1', 'NumberType': 'Unknown'}]
[{'PhoneNumber': '+14141120704', 'RequestProcessed': 'True', 'IsValid': 'No', 'NetworkCode': '', 'NetworkName': '', 'NetworkCountry': 'US', 'NationalFormat': '(414) 112-0704', 'CountryPrefix': '1', 'NumberType': 'Unknown'}]
[{'PhoneNumber': '+14140646454', 'RequestProcessed': 'True', 'IsValid': 'No', 'NetworkCode': '', 'NetworkName': '', 'NetworkCountry': 'US', 'NationalFormat': '(414) 064-6454', 'CountryPrefix': '1', 'NumberType': 'Unknown'}]
Click Enter For Exit ...!

**我在陣列 JSON 中通過旁路開括號“[] 完成它 **

感謝大家

圖片1

圖片2

有些人也可能需要幫助這是我跟進的線程的鏈接

如何在 Python 中的數組中打印特定值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM