简体   繁体   中英

How can I make my text not surrounded by ['']?

I am using the readlines feature in python + discord.py however my value in my embed is surrounded by ['']. I'd like to remove it from my embed for obvious reasons

            snipeduser = open("snipes/snipeduser.txt", "r").readlines()
            snipedchannel = open("snipes/snipedchannel.txt", "r").readlines()
            snipedserver = open("snipes/snipedserver.txt", "r").readlines()
            snipedmessage = open("snipes/snipedmessage.txt", "r").readlines()
            snipedmessagetime = open("snipes/snipedmessagetime.txt", "r").readlines()

            embed = discord.Embed(title="Message Sniped!", color=0xE71D36)
            embed.set_author(name=f"Message sent by {snipeduser}!")
            embed.add_field(name=f"Channel",value=f"{snipedchannel}", inline=True)
            embed.add_field(name=f"Server", value=f"{snipedserver}", inline=True)
            embed.add_field(name=f"Message", value=f"{snipedmessage}", inline=False)
            embed.set_footer(text=f"Time of Message Deletion {snipedmessagetime}")
            await ctx.send(embed=embed)

Btw, the code is working fine except for the [''] problem

Here's an example;

Message sent by ['My Discord ID (My Discord Tag)']!

Message Sniped!

Channel

['My Private Channel ID (bot-testing)']

Server

['My private server']

Message

['test']

Time of Message Deletion

['2020-11-11 21:34:08.330000']

The readlines() function, if you read the doc ( https://docs.python.org/3/tutorial/inputoutput.html ) returns a list . The list is the reason why you get the ['.....'] output.

So what you want is to get the first element of each list, for example snipeduser[0] instead of snipeduser .

Try this. :

with open(filename) as f:
    content = f.readlines()
# If you also want to remove whitespace characters like `\n` at the end of each line
text = [x.strip() for x in text] 

or

with open('filename') as f:
    lines = f.readlines()

I use join() for things like this. The join() string method returns a string by joining all the elements of an iterable, separated by a string separator, in this case just the separator is just a blank.

value=''.join(yourvaluehere)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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