【问题标题】:Getting error while replacing placeholders in Python在 Python 中替换占位符时出错
【发布时间】:2020-10-22 04:29:56
【问题描述】:

我最近使用了一个匿名文件共享网站anonymousfiles.io。它有一个 api。于是,我查了一下,发现

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

这段代码。我试图用这段代码制作一个小型控制台应用程序。

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')

这是我的代码。现在的问题是每当我执行它时,它都会显示一个错误提示

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
'{"url": "https://anonymousfiles.io/3ASBCuxh/", "id": "3ASBCuxh", "name": "Fumk.py", "size": 840}'

那么,如何在不出错的情况下替换占位符?

【问题讨论】:

  • 您正试图从文件的文本中访问文件属性。您将需要通过文件路径such as in this answer. 访问文件属性
  • Calimocho,不工作

标签: python string integer indices


【解决方案1】:

API 返回的响应将是 JSON 格式的字符串,因此您需要先将其转换为 python 字典,然后才能格式化打印的字符串。

这就是为什么它说TypeError: string indices must be integers 因为响应是一个字符串,而您正试图用另一个字符串对其进行索引。 (例如“你好”[“世界”])

我们可以使用python标准库中json包中的loads method来实现。

其次,API 不再返回文件的 mime-type,因此我将其删除。

试试这个:

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')

【讨论】:

  • 上传包含{"url": "https://anonymousfiles.io/3ASBCuxh/", "id": "3ASBCuxh", "name": "Fumk.py", "size": 840}
  • 导入时出现小错误,请确保第24行有:upload = loading(fu.text)
  • 另外,api 不再返回 mime-type 所以我已经删除了它
猜你喜欢
  • 1970-01-01
  • 2013-04-08
  • 1970-01-01
  • 2017-09-11
  • 2013-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多