Week 3 class demo
import pathlib
import glob
import csv
!pwd
/home/nbuser/library
for f in glob.glob('examplefolder/*.txt'):
print(f.split('/')[-1].split('.')[0])
demo
countsmalltext
uppersmalltext
smalltext
boomboom
target = pathlib.Path('examplefolder')
# paths = list(target.glob('*.txt'))
paths = target.glob('*.txt')
for f in paths:
print(f.stem)
demo
countsmalltext
uppersmalltext
smalltext
boomboom
nbs = pathlib.Path('.').glob('*.ipynb')
for f in nbs:
print(f.absolute())
/home/nbuser/library/week2notes-Copy.ipynb
/home/nbuser/library/Untitled.ipynb
/home/nbuser/library/week1.ipynb
/home/nbuser/library/testingtime.ipynb
/home/nbuser/library/test.ipynb
/home/nbuser/library/Week 2 demo.ipynb
/home/nbuser/library/file creation project.ipynb
/home/nbuser/library/week2notes.ipynb
/home/nbuser/library/Week3Notes.ipynb
/home/nbuser/library/week1classdemo.ipynb
target = list(pathlib.Path('examplefolder').glob('*'))
for f in target:
print(f.name, f.is_dir())
demo.txt False
countsmalltext.txt False
data True
uppersmalltext.txt False
smalltext.txt False
boomboom.txt False
folders = [f for f in target if f.is_dir()]
folders
[PosixPath('examplefolder/data')]
files = [f for f in target if f.is_file()]
files
[PosixPath('examplefolder/demo.txt'),
PosixPath('examplefolder/countsmalltext.txt'),
PosixPath('examplefolder/uppersmalltext.txt'),
PosixPath('examplefolder/smalltext.txt'),
PosixPath('examplefolder/boomboom.txt')]
categories = ['book', 'movie']
outtarget = pathlib.Path('results2')
if not outtarget.exists():
outtarget.mkdir()
for cat in categories:
for i in range(1, 16):
fname = cat + "-" + str(i).zfill(3) + '.txt'
out = outtarget / fname
out.write_text(cat + " data file " + str(i))
"hello".zfill(3)
'hello'
target = pathlib.Path('examplefolder') / pathlib.Path('boomboom.txt')
text = target.read_text()
print(type(text))
with open(str(target.absolute()), 'r', encoding = 'utf-8') as file_in:
print(type(file_in))
text = file_in.read()
print(type(text))
<class '_io.TextIOWrapper'>
<class 'str'>
with open(str(target.absolute()), 'r', encoding = 'utf-8') as file_in:
print(type(file_in))
lines = file_in.readlines()
lines
['A told B, and B told C, "I\'ll meet you at the top of the coconut tree."\n',
'"Wheee!" said D to E F G, "I\'ll beat you to the top of the coconut tree."\n',
'Chicka chicka boom boom! Will there be enough room? Here comes H up the coconut tree,\n',
'and I and J and tag-along K, all on their way up the coconut tree.\n',
"Chicka chicka boom boom! Will there be enough room? Look who's coming! L M N O P!\n",
'And Q R S! And T U V! Still more - W! And X Y Z!\n',
'The whole alphabet up the - Oh, no! Chicka chicka... BOOM! BOOM!\n',
'Skit skat skoodle doot. Flip flop flee. Everybody running to the coconut tree.\n',
'Mamas and papas and uncles and aunts hug their little dears, then dust their pants.\n',
'"Help us up," cried A B C.\n',
'Next from the pileup skinned-knee D and stubbed-toe E and patched-up F. Then comes G all out of breath.\n',
'H is tangled up with I. J and K are about to cry. L is knotted like a tie.\n',
'M is looped. N is stopped. O is twisted alley-oop. Skit skat skoodle doot. Flip flop flee.\n',
"Look who's coming! It's black-eyed P, Q R S, and loose-tooth T. Then U V W wiggle-jiggle free.\n",
'Last to come X Y Z. And the sun goes down on the coconut tree...\n',
"But - chicka chicka boom boom! Look, there's a full moon.\n",
'A is out of bed, and this is what he said, "Dare double dare, you can\'t catch me.\n',
'Chicka chicka BOOM! BOOM!Chicka chicka BOOM! BOOM!\n',
'I\'ll beat you to the top of the coconut tree."\n',
'Chicka chicka BOOM! BOOM!']
infile = open(str(target.absolute()), 'r')
text = infile.read()
infile.close()
print(type(text))
target = pathlib.Path('editeddate.csv')
import csv
with open(str(target.absolute()), 'r', encoding = 'utf-8') as file_in:
csvin = csv.reader(file_in)
headers = next(csvin)
data = [r for r in csvin]
data
[['2019-10-04', '.DS_Store', 'Urbana, IL'],
['2019-05-21', 'citation-312031600.txt', "'Chigago", " IL'"],
['2019-01-28', 'conda-cheatsheet.pdf', 'St. Simons Island, Ga.'],
['2018-04-12', 'meeting (1).collab', "'yes'"],
['2019-04-04', 'playerswithcoords.csv', "'yes'"],
['2019-10-04', 'readme', "'yes'"],
['2018-05-18', 'report.html', "'yes'"],
['2019-10-04', 'test', "'yes'"],
['2019-04-04', 'writtenfileinword.png', "'yes'"]]
target = pathlib.Path('ltdplayers.csv')
import csv
with open(str(target.absolute()), 'r', encoding = 'utf-8') as file_in:
csvin = csv.reader(file_in)
headers = next(csvin)
data = [r for r in csvin]
data
[['2018/morgan-brian.html',
'Morgan',
'Brian',
'Midfielder',
'1993-02-26T00:00:00Z',
'5-7',
'St. Simons Island, Ga.',
'Chicago Red Stars'],
['2018/jane-campbell.html',
'Jane',
'Campbell',
'Goalkeeper',
'1995-02-17T00:00:00Z',
'5-9',
'Kennesaw, Ga.',
'Houston Dash'],
['2018/abby-dahlkemper.html',
'Abby',
'Dahlkemper',
'Defender',
'1993-05-13T00:00:00Z',
'5-7',
'Menlo Park, Calif.',
'NC Courage'],
['2018/tierna-davidson.html',
'Tierna',
'Davidson',
'Defender',
'1998-09-19T00:00:00Z',
'5-10',
'Menlo Park, Calif.',
'Stanford'],
['2018/crystal-dunn.html',
'Crystal',
'Dunn',
'Defender',
'1992-07-03T00:00:00Z',
'5-1',
'Rockville Centre, N.Y.',
'NC Courage'],
['2018/julie-johnston.html',
'Julie',
'Ertz',
'Midfielder',
'1992-04-06T00:00:00Z',
'5-7',
'Mesa, Ariz.',
'Chicago Red Stars']]
print(headers)
fnamecol = headers.index('filename')
print(fnamecol)
['last_edited_date', 'filename', " 'hello", " world'"]
1
for row in data:
print(row[fnamecol], row[2])
.DS_Store 'Urbana
citation-312031600.txt 'Chigago
conda-cheatsheet.pdf yes
meeting (1).collab yes
playerswithcoords.csv yes
readme yes
report.html yes
test yes
writtenfileinword.png yes
with open('playerdata.csv', 'r') as fin:
csvin = csv.reader(fin)
headers = next(csvin)
playerdata = [r for r in csvin]
headers
['Filename',
'Firstname',
'Surname',
'Position',
'Birthdate',
'Height',
'Hometown',
'Club']
for r in playerdata:
print(r[headers.index('Hometown')])
St. Simons Island, Ga.
Kennesaw, Ga.
Menlo Park, Calif.
Menlo Park, Calif.
Rockville Centre, N.Y.
Mesa, Ariz.
Lincoln, Neb.
Satellite Beach, Fla.
Gilbert, Ariz.
Basking Ridge, N.J.
Golden, Colo.
Boise, Idaho
Cincinnati, Ohio
Delran, N.J.
Northport, N.Y.
Ventura, Calif.
Birmingham, Ala.
Chapin, S.C.
San Jose, Calif
Hanson, Mass.
Diamond Bar, Calif.
Stratford, Conn.
Fayetteville, Ga.
Palos Verdes Estates, Calif.
Highlands Ranch, Colo.
Redding, Calif.
Lake Forest, Calif.
St. Louis, Mo.
Naperville, Ill.
Fort Worth, Texas
Marietta, Ga.
Lorton, Va.
Fresno, Calif.
Camarillo, Calif.
Kennesaw, Ga.
Salina, Kan.
Satellite Beach, Fla.
Stratford, Conn.
allrows = []
linenum = 1
for row in lines:
splitrow = row.split()
numchars = len(row)
numwords = len(splitrow)
row = [linenum, numchars, numwords, "Urbana, IL, USA"]
allrows.append(row)
linenum += 1
headers = ['linenum', 'numchars', 'numwords', 'location']
allrows
[[1, 72, 17, 'Urbana, IL, USA'],
[2, 74, 17, 'Urbana, IL, USA'],
[3, 86, 16, 'Urbana, IL, USA'],
[4, 67, 15, 'Urbana, IL, USA'],
[5, 82, 17, 'Urbana, IL, USA'],
[6, 49, 16, 'Urbana, IL, USA'],
[7, 65, 12, 'Urbana, IL, USA'],
[8, 79, 13, 'Urbana, IL, USA'],
[9, 84, 15, 'Urbana, IL, USA'],
[10, 27, 7, 'Urbana, IL, USA'],
[11, 104, 19, 'Urbana, IL, USA'],
[12, 75, 19, 'Urbana, IL, USA'],
[13, 91, 17, 'Urbana, IL, USA'],
[14, 95, 18, 'Urbana, IL, USA'],
[15, 65, 15, 'Urbana, IL, USA'],
[16, 58, 11, 'Urbana, IL, USA'],
[17, 82, 18, 'Urbana, IL, USA'],
[18, 51, 7, 'Urbana, IL, USA'],
[19, 47, 10, 'Urbana, IL, USA'],
[20, 25, 4, 'Urbana, IL, USA']]
with open('linecounts.csv', 'w', encoding = 'utf-8') as file_out:
csvout = csv.writer(file_out)
csvout.writerow(headers)
csvout.writerows(allrows)
d = {'apple': 1, 'banana': 10, 'shopping': ['a', 'b', 'c', 'd'], 'happy': 'mostly'}
d
{'apple': 1, 'banana': 10, 'happy': 'mostly', 'shopping': ['a', 'b', 'c', 'd']}
import json
with open('shoppingdata.json', 'w') as jout:
json.dump(d, jout, indent = 4)
del d
d
NameError Traceback (most recent call last)
<ipython-input-147-e983f374794d> in <module>()
----> 1 d
NameError: name 'd' is not defined
with open('shoppingdata.json', 'r') as fin:
data = json.load(fin)
data['shopping']
['a', 'b', 'c', 'd']
f = pathlib.Path('shoppingdata.json')
record = f.read_text()
data = json.loads(record)
data
{'apple': 1, 'banana': 10, 'happy': 'mostly', 'shopping': ['a', 'b', 'c', 'd']}
import os
import pathlib
target = pathlib.Path('examplefolder').glob('*')
for f in target:
print(f.name, os.path.getmtime(str(f)))
demo.txt 1570474458.0
countsmalltext.txt 1570474457.0
data 1570474457.0
uppersmalltext.txt 1570474458.0
smalltext.txt 1570474458.0
boomboom.txt 1570474456.0
import time
print("wait for it")
time.sleep(5)
print("that was great")
wait for it
that was great
time.localtime()
time.struct_time(tm_year=2019, tm_mon=10, tm_mday=9, tm_hour=16, tm_min=38, tm_sec=35, tm_wday=2, tm_yday=282, tm_isdst=0)
now = time.gmtime()
print(time.strftime("%Y-%m-%d %H-%M in the %Z timezone", now))
2019-10-09 16-41 in the GMT timezone
import os
import pathlib
target = pathlib.Path('examplefolder').glob('*')
for f in target:
epocht = os.path.getmtime(str(f))
print(f.name, time.strftime("%Y-%m-%d", time.gmtime(epocht)))
demo.txt 2019-10-07
countsmalltext.txt 2019-10-07
data 2019-10-07
uppersmalltext.txt 2019-10-07
smalltext.txt 2019-10-07
boomboom.txt 2019-10-07
time.gmtime(1570474456.0)
time.struct_time(tm_year=2019, tm_mon=10, tm_mday=7, tm_hour=18, tm_min=54, tm_sec=16, tm_wday=0, tm_yday=280, tm_isdst=0)
Last updated
Was this helpful?