#๐ split
28 messages ยท Page 1 of 1 (latest)
@topaz onyx
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.
can you show what the line looks like?
with open('contact.txt') as f:
for line in f:
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']
Where did this data come from? Did you write this list to a file?
yes its in file
yes, but it's odd to have python list structure in a text file
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
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
so which way you recommend ?
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
i saw it in stackoverflow
i can delete it
but can you tell me the logic to create a new ?
file_text.write(f'{first_name},{last_name},{phone_number},{age},{gender},{city}\n')
you can write it as an f-string like so
ooo lemme try
thanks for help 
Are you sure what you saw on Stack Overflow wasn't
print(first_name,last_name,phone_number,age,gender,city,"\n", file=file_text)
?
ahh good call!
I only suggest that because I've seen this confusion before.
More than once actually.
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.