I'm creating a discord bot in python and I want to have a log system for the warn command. To set up this, I'm using a .csv file in which I write all the information I want about a user.
It's the first time I use .csv, so to manipulate the data, I take the content of the file and convert it into a list of list. One sublist = one line of the table.
After treatment, I put all the elements of each sublists side by side, separated by a ";" to store it properly in the .csv file.
The problem I have comes from this :
csvwriter = csv.writer(wfile, delimiter=";")
For each caracter in the final file, a ";" is put right after so instead of this : (abc;def;ghi), I have this : (a;b;c;d;...).
All of the code :
@commands.command()
@commands.has_any_role(765280296781742090, 765609632482721852, 882222368976670772)
async def test(self, ctx, member:discord.Member):
messages = await ctx.channel.history(limit = 1).flatten()
for each_message in messages:
await each_message.delete()
with open("ressources/logs/warn_logs.csv", 'r', newline='') as csvfile:
content = []
new_registrated = []
is_registrated = False
for line in csvfile:
line = line.split(";")
content.append(line)
for cell in content:
if cell == content[0]:
pass
else:
if int(cell[0]) == int(member.id):
is_registrated = True
cell[3] = int(cell[3]) 1
cell[3] = str(cell[3])
if is_registrated == False:
new_registrated.append(str(member.id))
new_registrated.append(str(member._user))
new_registrated.append(member.nick)
new_registrated.append("1\r\n")
content.append(new_registrated)
with open("ressources/logs/warn_logs.csv", 'w', newline='') as wfile:
csvwriter = csv.writer(wfile, delimiter=";")
# for line in content:
# print(line)
# line = ";".join(line)
# print(line)
# csvwriter.writerow(line)
csvwriter.writerow("abc")
wfile.close()
csvfile.close()
I'm working on it for a while now so if someone could help me, I would be very greatful.
NB: I'm French, and my English isn't perfect so don't hesitate to correct me
I tried to:
- Not put the delimiter : the ";" became ","
- Put delimiter="smth" / =False / =None : it doesn't work
- Any other character : ";" became any other character
CodePudding user response:
The writerow()
function expects an iterable object to write a whole row.
csvwriter.writerow("abc")
will write to the file:
a;b;c
and
csvwriter.writerow(["abc", "def"])
will write:
abc;def
If line
is a list of strings then line = ";".join(line)
is wrong, line
should go directly to writerow()
. The whole point of using a csv
module in not to think about delimiters.
Also if you are using a with open(...) as f:
you don't have to close the file.
CodePudding user response:
I updated my code with your advices :
with open("ressources/logs/warn_logs.csv", 'r', newline='') as rfile:
csv_content = rfile.readlines()
py_content = []
cell = []
is_registrated = False
# print(csv_content)
for line in csv_content:
line = line.split(";")
py_content.append(line)
print(py_content)
for cell in py_content:
if cell != py_content[0]:
if int(cell[0]) == int(member.id):
is_registrated = True
cell[-1] = str(int(cell[-1]) 1)
if is_registrated == False:
new_registrated = [str(member.id),str(member._user),str(member.nick),"1"]
py_content.append(new_registrated)
# print(py_content)
rfile.close()
# print(py_content)
with open("ressources/logs/warn_logs.csv", 'w', newline='') as wfile:
csvwriter = csv.writer(wfile, delimiter=";")
for cell in py_content:
# print(cell)
csvwriter.writerow(cell)
wfile.close()
But in my .csv file, obtain this :
I don't know how to remove the quotes and the line break