%run talktools.py import string dir(string) s = "HeLLo tHEre MY FriEND" s.upper() s.lower() s.title() s.capitalize() s.swapcase() s.split() L = s.capitalize().split() L s = '_'.join(L) s s.split('_') ''.join(s.split('_')) s = " Too many spaces! " s.strip() s = "*~*~*~*Super!!**~*~**~*~**~" s.strip('*~') s.rstrip('*~') s.lstrip('*~') s.replace('*', '') s.replace('*', '').replace('~', '') s = "The quick brown fox jumped" s.find("fox") s[16:] s.find('booyah') s.startswith('The') s.endswith('jumped') s.endswith('fox') '1234'.isdigit() '123.45'.isdigit() 'ABC'.isalpha() 'ABC123'.isalpha() "ABC123".isalnum() 'ABC easy as 123'.isalnum() 'hello'.islower() 'HELLO'.isupper() 'Hello'.istitle() ' '.isspace() from math import pi "my favorite integer is %d, but my favorite float is %f." % (42, pi) "in exponential notation it's %e" % pi "to choose smartly if exponential is needed: %g" % pi "or with a bigger number: %g" % 123456787654321.0 "rounded to three decimal places it's %.3f" % pi "an integer padded with spaces: %10d" % 42 "an integer padded on the right: %-10d" % 42 "an integer padded with zeros: %010d" % 42 "we can also name our arguments: %(value)d" % dict(value=3) "Escape the percent sign with an extra symbol: the %d%%" % 99 "{}{}".format("ABC", 123) "{0}{1}".format("ABC", 123) "{0}{0}".format("ABC", 123) "{1}{0}".format("ABC", 123) ("%.2f" % 3.14159) == "{:.2f}".format(3.14159) "{0:d} is an integer; {1:.3f} is a float".format(42, pi) "{the_answer:010d} is an integer; {pi:.5g} is a float".format(the_answer=42, pi=pi) '{desire} to {place}'.format(desire='Fly me', place='The Moon') # using a pre-defined dictionary f = {"desire": "Won't you take me", "place": "funky town?"} '{desire} to {place}'.format(**f) # format also supports binary numbers "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42) %%file inout.dat Here is a nice file with a couple lines of text it is a haiku f = open('inout.dat') print(f.read()) f.close() f = open('inout.dat') print(f.readlines()) f.close() for line in open('inout.dat'): print(line.split()) # write() is the opposite of read() contents = open('inout.dat').read() out = open('my_output.dat', 'w') out.write(contents.replace(' ', '_')) out.close() !cat my_output.dat # writelines() is the opposite of readlines() lines = open('inout.dat').readlines() out = open('my_output.dat', 'w') out.writelines(lines) out.close() !cat my_output.dat # Don't modify this: it simply writes the example file f = open('messy_data.dat', 'w') import random for i in range(100): for j in range(5): f.write(' ' * random.randint(0, 6)) f.write('%0*.*g' % (random.randint(8, 12), random.randint(5, 10), 100 * random.random())) if j != 4: f.write(',') f.write('\n') f.close() # Look at the first four lines of the file: !head -4 messy_data.dat # your solution here import numpy as np data = np.loadtxt("messy_data.dat", delimiter=',') np.savetxt("clean_data.dat", data, delimiter=',', fmt="%8.4f") !head -5 clean_data.dat