简体   繁体   English

替换 Python 中的占位符时出错

[英]Getting error while replacing placeholders in Python

I recently used an anonymous file sharing site anonymousfiles.io.我最近使用了一个匿名文件共享网站anonymousfiles.io。 It has an api.它有一个 api。 So, I looked into it and found所以,我调查了一下,发现

import requests
r =  requests.post('https://api.anonymousfiles.io', files={'file': open('#1.txt', 'rb')})
print(r.text)

this code.这段代码。 I tried to make a small console app with this peice of code.我试图用这段代码制作一个小型控制台应用程序。

print()
print('ANONYMOUSFILES.IO - File Uploader(API)')
print()
print()

import requests

try:
    while True:
        print('Enter the file path for upload')
        file_path = input(':-')

        if file_path == '' or file_path == ' ':
            print()
            print()
            print('Please enter a valid file path')
        else:
            fu =  requests.post('https://api.anonymousfiles.io', files={'file': open(file_path, 'rb')})
            upload=fu.text
            print('''File Name: {0}
                     File Size: {1}
                     File Type: {2}

                     **********

                     {4}
                  '''.format(upload['name'],upload['size'],upload['mime_type'],upload['url']))

except FileNotFoundError:
    print('Please enter a valid file path')

This is my code.这是我的代码。 Now the problem is whenever I execute it, it shows an error saying现在的问题是每当我执行它时,它都会显示一条错误消息

Traceback (most recent call last):
  File "I:\Python Exports\Fumk.py", line 27, in <module>
    '''.format(upload['name'],upload['size'],upload['mime_type'],upload['url']))
TypeError: string indices must be integers

upload cointains上传硬币

>>> upload
'{"url": "https://anonymousfiles.io/3ASBCuxh/", "id": "3ASBCuxh", "name": "Fumk.py", "size": 840}'

So, how do I replace the placeholders without getting an error?那么,如何在不出错的情况下替换占位符?

The response returned by the API will be a string in JSON format, so you will need to convert this into a python dictionary before you can format your printed string. API 返回的响应将是 JSON 格式的字符串,因此您需要将其转换为 python 字典,然后才能格式化打印的字符串。

That is why it says TypeError: string indices must be integers because the response is a string and you are trying to index it with another string.这就是为什么它说TypeError: string indices must be integers因为响应是一个字符串,而您正试图用另一个字符串索引它。 (eg "Hello"["World"]) (例如“你好”[“世界”])

We can use the loads method from the json package in the python standard library to achieve this.我们可以使用 python 标准库中 json package 中的加载方法来实现这一点。

Secondly, the API no longer returns the mime-type of the file, so I have removed this.其次,API 不再返回文件的 mime-type,所以我把它去掉了。

Try this:尝试这个:

print()
print('ANONYMOUSFILES.IO - File Uploader(API)')
print()
print()


## for loading a JSON string into a dictionary
from json import loads

import requests

try:
    while True:
        print('Enter the file path for upload')
        file_path = input(':-')

        if file_path == '' or file_path == ' ':
            print()
            print()
            print('Please enter a valid file path')
        else:
            fu =  requests.post('https://api.anonymousfiles.io', files={'file': open(file_path, 'rb')})
            # Convert to a dictionary before parsing
            upload = loads(fu.text)
            print('''
                     File Name: {0}
                     File Size: {1}

                     **********

                     {2}
                  '''.format(upload['name'],upload['size'],upload['url']))

except FileNotFoundError:
    print('Please enter a valid file path')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM