如何使用python添加文本文件?
基本上,我正在为我的discord机器人制作一个货币系统,我正在试图找出如何在文本文件中添加一个数字,比如如果我做^work(前缀+消息),它会在文本文件中添加+100,这是我的代码,顺便说一下,用户名=message.author.name如何使用python添加文本文件?,python,discord.py,Python,Discord.py,基本上,我正在为我的discord机器人制作一个货币系统,我正在试图找出如何在文本文件中添加一个数字,比如如果我做^work(前缀+消息),它会在文本文件中添加+100,这是我的代码,顺便说一下,用户名=message.author.name if (str(username)) in f.read(): with open("currency.txt", "r") as f: lines = f.readlines()
if (str(username)) in f.read():
with open("currency.txt", "r") as f:
lines = f.readlines()
with open("currency.txt", "r") as f:
for line in lines:
if line.strip("\n") != (str(username)):
(+100)
else:
f = open ("currency.txt","a")
f.write (str(username)+("+100"))
f.write ("\n")
f.close()
尝试创建一个json文件(您可以创建一个全局变量,这样您就可以在任何地方使用它),因此您应该使用@client.event
,当bot联机时,它会启动
@client.event
async def on_ready():
global currency #Creates the global variable
try:
with open('currency.json') as f: #Opens json file
currency = json.load(f)
要保存json文件,您必须创建一个带有的定义,以便可以在任何地方调用它
def _save():
with open('currency.json', 'w+') as f: #Opens the file
json.dump(currency, f) #Dumps the json file
如果要在其中存储数字,它必须是字符串,执行此操作的命令如下所示:
@client.command()
async def register(ctx):
id = str(ctx.message.author.id) #Defines the author ID (Always a string!)
if id not in currency: #Checks if the ID is already on the json file
currency[id] = 500 #Sets the currency
_save() #Saves the json file
else:
await ctx.send("You already have an account :x:")
@client.event
async def on_ready():
global currency #Creates the global variable
try:
with open('currency.json') as f: #Opens json file
currency = json.load(f)
@client.command()
async def register(ctx):
id = str(ctx.message.author.id) #Defines the author ID (Always a string!)
if id not in currency: #Checks if the ID is already on the json file
currency[id] = 500 #Sets the currency
_save() #Saves the json file
else:
await ctx.send("You already have an account :x:")
def _save():
with open('currency.json', 'w+') as f: #Opens the file
json.dump(currency, f) #Dumps the json file
所以最终的代码应该是这样的:
@client.command()
async def register(ctx):
id = str(ctx.message.author.id) #Defines the author ID (Always a string!)
if id not in currency: #Checks if the ID is already on the json file
currency[id] = 500 #Sets the currency
_save() #Saves the json file
else:
await ctx.send("You already have an account :x:")
@client.event
async def on_ready():
global currency #Creates the global variable
try:
with open('currency.json') as f: #Opens json file
currency = json.load(f)
@client.command()
async def register(ctx):
id = str(ctx.message.author.id) #Defines the author ID (Always a string!)
if id not in currency: #Checks if the ID is already on the json file
currency[id] = 500 #Sets the currency
_save() #Saves the json file
else:
await ctx.send("You already have an account :x:")
def _save():
with open('currency.json', 'w+') as f: #Opens the file
json.dump(currency, f) #Dumps the json file
PD:文件currency.json
中应该包含以下内容:
{}
让我知道这是否有用 问题出在哪里?要添加,我们是否应该在追加模式下打开…?我们没有使用列表,因此不,我们不应该使用.append(),因为它不是必需的。此外,我们正在使用用户ID,因为用户名可能会更改。