#๐Ÿ”’ split

28 messages ยท Page 1 of 1 (latest)

topaz onyx
#
inner_list = [elt.strip() for elt in line.split(',')]

hey i want to split the [ and ] how to do that ?

fast oasisBOT
#

@topaz onyx

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

formal adder
#

can you show what the line looks like?

topaz onyx
formal adder
#

that doesn't tell us what line looks like

topaz onyx
# formal adder that doesn't tell us what line looks like
read_contact_list = []
def get_user_from_file():
    with open('contact.txt') as f:
        for line in f:
            inner_list = [elt.strip() for elt in line.split(',')]
            read_contact_list.append(inner_list)
            print(inner_list[1])

this is the fullt code
and this is a sample of what in contact.txt
['radin', 'anti', '0999335', 17, 'male', 'LA']

formal adder
#

Where did this data come from? Did you write this list to a file?

formal adder
#

yes, but it's odd to have python list structure in a text file

#

did you write the code that created that file?

topaz onyx
# formal adder did you write the code that created that file?

yes

def create_user_to_file(first_name: str, last_name: str, phone_number: str, age: int, gender: str, city: str):
        file_text = open("contact.txt", "a")
        file_text.write(first_name,last_name,phone_number,age,gender,city,"\n")
        file_text.close()
        return first_name,last_name,phone_number,age,gender,city

this is the code
i want to create it as a list to call the first_name or last_name

formal adder
#

I would not write a list to a file like you are doing

#
file_text.write(first_name,last_name,phone_number,age,gender,city,"\n")
#

This should be an error

#

write only takes 1 string argument

topaz onyx
formal adder
#

well it's hard to say because the code you're sharing would error, so I don't actually know what you're doing

#

but basically the issue is you've stored the data in a txt file in a way you shouldn't have, and now you're trying to clean it up when you read the file, instead of not creating a problem with the txt file in the first place

topaz onyx
formal adder
#
file_text.write(f'{first_name},{last_name},{phone_number},{age},{gender},{city}\n')
#

you can write it as an f-string like so

topaz onyx
edgy cliff
edgy cliff
#

More than once actually.

fast oasisBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.