#๐ making text file
127 messages ยท Page 1 of 1 (latest)
@hybrid void
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.
Are you asking about how to make the text file? Or how to read it with python?
do you have examples of atomic weights?
H = 1.008
O = 15.999
C = 12.11
Ok great!
If you had this as a string, do you know how you'd split it up into "element" and "value"?
"H = 1.008"
through a dictionary?
a dictionary would be great to hold the results, but we still need to "parse" the string
we need a way to separate the element and weight out because right now it's just a single string
atomic = "H = 1.008"
You haven't seen a string before?
the 2nd equal sign is just part of the string
it doesn't mean anything, it's just a literal =
exactly
you could use whatever symbol you wanted really, as long as your consistent
atomic = "H | 1.008"
you could do this instead
ic
atomic = "H|1.008"
H|1.008
O|15.999
C|12.11
so if you have a text file that looks something like this, we can read the file line by line, split the |, then add a new key/value pair to a dictionary based on that result
ok ic
colon might look more natural in the text file though
how would i make a file?
or a comma, if we want to stick with more of a csv format
you don't need to make the file with python
make a new txt file on your computer and fill it in with the info
file_path = "new_file.txt"
with open(file_path, 'w') as file:
i read something like this, not examtly sure how it works. reason im asking is because duriong the exam im not sure ill be able to make oneseperately since ill be in a safe exam browser
perhaps you'll be provided the file then
for now, just create the file and then I can show you how to read it
yup exactly
open() is the function
with is a keyword for a context manager
i see thanks for the help, ill try to complete the rest of the program myself and will just post here if i get stuck
yes, only that it might be too visually similar to a period when reading it for a human to scan the content quickly
valid point
TSV it is ๐
this help thread will be automatically archived as read-only by the Python bot one hour after the last message was sent to the thread, so if it taks you longer then that you will either have to bump the thread before that until you are done with it, but often it's better to just open a new help thread instead if/when you need it
ah i see thanks for the heads up
i tried the split method for the textfile but i get an error message when using the (,) for some reason:
with open(atoms_file, 'r') as file:
for line in file:
atom, weight = line.split(,)
atom_weights[atom] = float(weight)
strings always need "" around them
If it's inside the string, so the "", it's just normal text.
The second = does not affect the code at all, think about it as a normal text.
So basically you have a variable, which is atomic and assigned a string/text to it.
oh wait
smh
old message
mb gng
what do you mean
like in this context
split() needs a string as its argument
the thing you put inside the () is called an argument
oh i thought split meant what character signifies a split to be made
so is line not an str in this scenario is what u mean?
ok i think ive finished the calculations part of the program, however i get the error message
FileNotFoundError: [Errno 2] No such file or directory: 'atoms.txt'
when i run the program.
it says the problem is on this line:
with open(atoms_file, 'r') as file:
this is the previous line which im thinking might have something to do with it but im not sure:
atoms_file = "atoms.txt"
any ideas or should i send more of the code?
where is the atoms.txt file?
like right next to my python file in the vs code folder thingy
theyre in the same folder
I don't use VSC, but I know it does some strange things regarding your cwd (current working directory)
yeah, when you launch a program, relative files always need to be relative to your CWD
usually it's set to whatever folder the file you're running is in
you can force it with a little trick though
import os
os.chdir(os.path.dirname(__file__))
put this at the top of your file
do you know what a key is?
yh the first part of the dict
a key error is when you try and look up a key in a dict that doesn't exist
!e
empty_dict = {}
print(empty_dict['H'])
:x: Your 3.14 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File [35m"/home/main.py"[0m, line [35m2[0m, in [35m<module>[0m
003 | print([31mempty_dict[0m[1;31m['H'][0m)
004 | [31m~~~~~~~~~~[0m[1;31m^^^^^[0m
005 | [1;35mKeyError[0m: [35m'H'[0m
oh ic
now it's up to you to figure out why that might be the case
print is your best friend for debugging things
print out the dict and see why it might be missing that key
weird my dict is empty
im printing right after i first add to them
atom_weights[atom] = float(weight) where atom_weights is the dict
print out some variables. Always think in your head "I expect it to look like this" and if it doesn't match what you think, you need to work backwards to figure out what might be the issue
show your full code
!paste
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
import os
os.chdir(os.path.dirname(file))
def m_mass(molecule):
atom_weights = {}
atoms_file = "atoms.txt"
with open(atoms_file, 'r') as file:
for line in file:
atom, weight = line.split(",")
atom_weights[atom] = float(weight)
print (atom_weights)
m_mass('C')
Hey @hybrid void!
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
use the paste link or add the discord markdown so I can properly read your code
!paste
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
can you share the txt file too?
my guess is you have a space before/after the comma
although that shouldn't result in an empty dict...
I had suggest change print(atom_weights) to return atom_weight for reusibility
perhaps you never saved the file
!p
pep_number
yeah, you should only be generating this atom dict once
you have = in the file, but you're trying to split using ,
You can drop the files into the past thingy brother no need to type
but you also should not have a= in the txt file
yh i changed it to C,numbers
and so on
oh nice now it works ahhaha finally thanks for all the help
This help channel has been closed. 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.