替换 Python 中的占位符时出错
Getting error while replacing placeholders in Python
我最近使用了一个匿名文件共享网站 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}'
那么,如何在不出现错误的情况下替换占位符?
API 返回的响应将是 JSON 格式的字符串,因此您需要先将其转换为 python 字典,然后才能格式化打印的字符串。
这就是它说 TypeError: string indices must be integers
的原因
因为响应是一个字符串,而您正试图用另一个字符串对其进行索引。 (例如“你好”[“世界”])
我们可以使用 python 标准库中 json 包中的 loads method 来实现这一点。
其次,API 不再是 returns 文件的 mime 类型,所以我删除了它。
试试这个:
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')
我最近使用了一个匿名文件共享网站 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}'
那么,如何在不出现错误的情况下替换占位符?
API 返回的响应将是 JSON 格式的字符串,因此您需要先将其转换为 python 字典,然后才能格式化打印的字符串。
这就是它说 TypeError: string indices must be integers
的原因
因为响应是一个字符串,而您正试图用另一个字符串对其进行索引。 (例如“你好”[“世界”])
我们可以使用 python 标准库中 json 包中的 loads method 来实现这一点。
其次,API 不再是 returns 文件的 mime 类型,所以我删除了它。
试试这个:
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')