<jupyter_start><jupyter_text>Structures like these are encoded in "PDB" files ![pdb_atoms](https://raw.githubusercontent.com/harmsm/pythonic-science/master/chapters/03_dealing-with-files/data/pdb_header.png) ![pdb_atoms](https://raw.githubusercontent.com/harmsm/pythonic-science/master/chapters/03_dealing-with-files/data/pdb_atoms.png)  How can we parse a complicted file like this one?<jupyter_code>import pandas as pd
pd.read_table("data/1stn.pdb")<jupyter_output><empty_output><jupyter_text>We can do better by manually *parsing* the file.  Our test file![pdb_atoms](https://raw.githubusercontent.com/harmsm/pythonic-science/master/chapters/03_dealing-with-files/data/test-file.png)  Predict what this will print<jupyter_code>f = open("test-file.txt")
print(f.readlines())
f.close()<jupyter_output><empty_output><jupyter_text>Predict what this will print<jupyter_code>f = open("test-file.txt")
for line in f.readlines():
    print(line)
f.close()<jupyter_output><empty_output><jupyter_text>Predict what this will print<jupyter_code>f = open("test-file.txt")
for line in f.readlines():
    print(line,end="")
f.close()<jupyter_output><empty_output><jupyter_text>Basic file reading operations: + Open a file for reading: `f = open(SOME_FILE_NAME)` + Read lines of file sequentially: `f.readlines()`+ Read one line from the file: `f.readline()`+ Read the whole file into a string: `f.read()`+ Close the file: `f.close()`  Now what do we do with each line?  Predict what the following program will do<jupyter_code>f = open("test-file.txt")
for line in f.readlines():
    print(line.split())
f.close()<jupyter_output><empty_output><jupyter_text>Predict what the following program will do<jupyter_code>f = open("test-file.txt")
for line in f.readlines():
    print(line.split("1"))
f.close()<jupyter_output><empty_output><jupyter_text>Splitting strings+ `SOME_STRING.split(CHAR_TO_SPLIT_ON)` allows you to split strings into a list.  + If `CHAR_TO_SPLIT_ON` is not defined, it will split on *all* whitespace (" ","\t","\n","\r")+ "\t" is TAB, "\n" is NEWLINE, "\r" is CARRIAGE_RETURN.   Predict what the following will do![test_file](https://raw.githubusercontent.com/harmsm/pythonic-science/master/chapters/03_dealing-with-files/data/test-file.png)<jupyter_code>f = open("test-file.txt")
lines = f.readlines()
f.close()

line_of_interest = lines[-1]
value = line_of_interest.split()[0]
print(value)<jupyter_output><empty_output><jupyter_text>Predict what will happen:<jupyter_code>print(value*5)<jupyter_output><empty_output><jupyter_text>`value` is a string of "1.5".  You can't do math on it yet.   The solution is to *cast* it into a float<jupyter_code>value_as_float = float(value) 
print(value_as_float*5)<jupyter_output><empty_output><jupyter_text>Cast calls:`float`, `int`, `str`, `list`, `tuple`<jupyter_code>list("1.5")<jupyter_output><empty_output><jupyter_text>Write a program that grabs the "1" from the first line in the file and multiplies it by 75.<jupyter_code>f = open("test-file.txt")
lines = f.readlines()
f.close()

value = lines[0].split(" ")[1]
value_as_int = int(value)
print(value_as_int*75)<jupyter_output><empty_output><jupyter_text>What about *writing* to files?  Basic file writing operations: + Open a file for writing: `f = open(SOME_FILE_NAME,'w')` **will wipe out file immediately!**+ Open a file to append: `f = open(SOME_FILE_NAME,'a')`+ Write a string to a file: `f.write(SOME_STRING)`+ Write a list of strings: `f.writelines([STRING1,STRING2,...])`+ Close the file: `f.close()`<jupyter_code>def file_printer(file_name):
    f = open(file_name)
    for line in f.readlines():
        print(line,end="")
    f.close()<jupyter_output><empty_output><jupyter_text>Predict what this code will do<jupyter_code>a_list = ["a","b","c"]
f = open("another-file.txt","w")
for a in a_list:
    f.write(a)
f.close()
file_printer("another-file.txt")<jupyter_output><empty_output><jupyter_text>Predict what this code will do<jupyter_code>a_list = ["a","b","c"]
f = open("another-file.txt","w")
for a in a_list:
    f.write(a)
    f.write("\n")
f.close()
file_printer("another-file.txt")<jupyter_output><empty_output><jupyter_text>Predict what this code will do<jupyter_code>a_list = ["a","b","ccat"]
f = open("another-file.txt","w")
for a in a_list:
    f.write("A test {{}} {}\n".format(a))
f.close()
file_printer("another-file.txt")<jupyter_output><empty_output><jupyter_text>`format` lets you make pretty strings<jupyter_code>print("The value is: {:}".format(10.35151))
print("The value is: {:.2f}".format(10.35151))
print("The value is: {:20.2f}".format(10.35151))
print("The value is: {:}".format(10))
print("The value is: {:20d}".format(10))<jupyter_output><empty_output><jupyter_text>String formatting+ Pretty decimal printing: `"{:LENGITH_OF_STRING.NUM_DECIMALSf}".format(FLOAT)`+ Pretty integer printing: `"{:LENGTH_OF_STRINGd}".format(INT)`+ Pretty string printing:  `"{:LENGTH_OF_STRINGs}".format(STRING)`  Create a loop that prints 0 to 9 to a file.  Each number should be on its own line, written to 3 decimal places.<jupyter_code>f = open("junk","w")
for i in range(10):
    f.write("{:.3f}\n".format(i))
f.close()
file_printer("junk")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16066961708678085, 'fraction_numerical': 0.012314796998268231, 'mean_word_length': 6.610541727672035, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 69, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '306151', 'license': 'Unlicense', 'n_tokens_mistral': 1995, 'n_tokens_neox': 1940, 'n_words': 410, 'path': 'chapters/03_dealing-with-files/00_interacting-with-files_key.ipynb', 'repo_name': 'lperezmo/pythonic-science', 'size': '5197'}