Skip to content

Instantly share code, notes, and snippets.

@deepakshrma
Last active March 20, 2022 17:26
Show Gist options
  • Save deepakshrma/b87c5d9d1382bdb1a2bce1b2b03bcabc to your computer and use it in GitHub Desktop.
Save deepakshrma/b87c5d9d1382bdb1a2bce1b2b03bcabc to your computer and use it in GitHub Desktop.
Reading Data with Python | Easy Learning
# Read entire file one time
page = ""
with open("app.log", 'r') as file:
page = file.read()
print(page)
"""
# Output
03/22 08:51:06 TRACE :...read_physical_netif: Home list entries returned = 7
03/22 08:51:06 INFO :...read_physical_netif: index #0, interface VLINK1 has address 129.1.1.1, ifidx 0
...
"""
print("*" * 80)
# Read lines
lines = []
with open("app.log", 'r') as file:
lines = file.readlines()
print(lines[0])
"""
# Output
03/22 08:51:06 TRACE :...read_physical_netif: Home list entries returned = 7
"""
# Read line by line
rows = []
with open("app.log", 'r') as file:
index = 0
for line in file:
date, time, type, *rest = line.split()
rows.append(
{
"index": index,
"date": date,
"time": time,
"type": type,
"line": " ".join(rest),
}
)
index += 1
print(rows[2])
"""
# Output
{'index': 2, 'date': '03/22', 'time': '08:51:06', 'type': 'INFO', 'line': ':...read_physical_netif: index #1, interface TR1 has address 9.37.65.139, ifidx 1'}
"""
import csv
with open("country.csv", "r") as file:
csv_reader = csv.reader(file)
for line in csv_reader:
print(line)
"""
# Output
['name', 'area', 'country_code2', 'country_code3']
['Afghanistan', '652090.00', 'AF', 'AFG']
['Albania', '28748.00', 'AL', 'ALB']
...
"""
import csv
rows = []
with open("country.csv", "r") as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
rows.append(row)
print(rows[2])
"""
# Output
{'name': 'Algeria', 'area': '2381741.00', 'country_code2': 'DZ', 'country_code3': 'DZA'}
"""
import csv
import io
from typing import Any
from urllib.request import urlopen
oscar_age_female_url = (
"https://people.sc.fsu.edu/~jburkardt/data/csv/oscar_age_female.csv"
)
def requestCSV(url: str, default: list[Any] = []) -> list[dict[str, str]]:
## A function take url to read csv file and
## return list of dictionary rows in it
data = default
with urlopen(url) as response:
## Open an html url
io_reader = io.StringIO(response.read().decode())
reader = csv.reader(io_reader, skipinitialspace=True)
## Read csv and convert to dictionary, strip extra space
for row in reader:
data.append(row)
return data
rows = requestCSV(oscar_age_female_url)
print(rows[0]) ## Headers
print(rows[1:3]) ## 2,3 rows
"""
# Output
['Index', 'Year', 'Age', 'Name', 'Movie']
[
['1', '1928', '22', 'Janet Gaynor', 'Seventh Heaven, Street Angel and Sunrise: A Song of Two Humans'],
['2', '1929', '37', 'Mary Pickford', 'Coquette']
]
"""
import csv
import io
from typing import Any
from urllib.request import urlopen
oscar_age_female_url = (
"https://people.sc.fsu.edu/~jburkardt/data/csv/oscar_age_female.csv"
)
def requestJsonCSV(url: str, default: list[Any] = []) -> list[dict[str, str]]:
## A function take url to read csv file and
## return list of dictionary rows in it
data = default
with urlopen(url) as response:
## Open an html url
io_reader = io.StringIO(response.read().decode())
reader = csv.DictReader(io_reader, skipinitialspace=True)
## Read csv and convert to dictionary, strip extra space
for row in reader:
data.append(row)
return data
rows = requestJsonCSV(oscar_age_female_url)
print(rows[-2:])
"""
# Output
[
{'Index': '88', 'Year': '2015', 'Age': '54', 'Name': 'Julianne Moore', 'Movie': 'Still Alice'},
{'Index': '89', 'Year': '2016', 'Age': '26', 'Name': 'Brie Larson', 'Movie': 'Room'}
]
"""
import sqlite3
rows = []
with sqlite3.connect("test.db") as db:
for row in db.execute(
"SELECT name, cast(population as unsigned) \
FROM cities \
ORDER BY cast(population as unsigned) DESC"
):
rows.append(row)
print(rows[:3])
"""
[('Albuquerque', 448607), ('Arlington', 332969), ('Anaheim', 328014)]
"""
## most populated city
print(rows[0])
"""
('Albuquerque', 448607)
"""
03/22 08:51:06 TRACE :...read_physical_netif: Home list entries returned = 7
03/22 08:51:06 INFO :...read_physical_netif: index #0, interface VLINK1 has address 129.1.1.1, ifidx 0
03/22 08:51:06 INFO :...read_physical_netif: index #1, interface TR1 has address 9.37.65.139, ifidx 1
03/22 08:51:06 INFO :...read_physical_netif: index #2, interface LINK11 has address 9.67.100.1, ifidx 2
03/22 08:51:06 INFO :...read_physical_netif: index #6, interface LOOPBACK has address 127.0.0.1, ifidx 0
03/22 08:51:06 INFO :...mailbox_register: mailbox allocated for timer
name population
Abilene 115930
Akron 217074
Albany 93994
Albuquerque 448607
Alexandria 128283
Allentown 106632
Amarillo 173627
Anaheim 328014
Anchorage 260283
Ann Arbor 114024
Arden-Arcade 92040
Arlington 332969
Arlington 174838
Arvada 102153
Athens-Clarke County 101489
Atlanta 416474
Augusta-Richmond County 199775
Aurora 276393
Aurora 142990
Austin 656562
Bakersfield 247057
Baltimore 651154
Baton Rouge 227818
Beaumont 113866
Bellevue 109569
Berkeley 102743
Billings 92988
Birmingham 242820
Boise City 185787
Boston 589141
Boulder 91238
Bridgeport 139529
Brockton 93653
Brownsville 139722
Buffalo 292648
Burbank 100316
Cambridge 101355
Cape Coral 102286
Carrollton 109576
Carson 89089
Cary 91213
Cedar Rapids 120758
Chandler 176581
Charleston 89063
Charlotte 540828
Chattanooga 155554
Chesapeake 199184
Chicago 2896016
Chula Vista 173556
Cincinnati 331285
Citrus Heights 103455
Clarksville 108787
Clearwater 99936
Cleveland 478403
Colorado Springs 360890
Columbia 116278
Columbus 711470
Columbus 186291
Compton 92864
Concord 121780
Coral Springs 117549
Corona 124966
Corpus Christi 277454
Costa Mesa 108724
Dallas 1188580
Daly City 103621
Davenport 98256
Dayton 166179
Denver 554636
Des Moines 198682
Detroit 951270
Downey 107323
Durham 187035
East Los Angeles 126379
El Cajon 94578
El Monte 115965
El Paso 563662
Elgin 89408
Elizabeth 120568
Erie 103717
Escondido 133559
Eugene 137893
Evansville 121582
Fairfield 92256
Fall River 90555
Fayetteville 121015
Flint 124943
Fontana 128929
Fort Collins 118652
Fort Lauderdale 152397
Fort Wayne 205727
Fort Worth 534694
Fremont 203413
Fresno 427652
Fullerton 126003
Gainesville 92291
Garden Grove 165196
Garland 215768
Gary 102746
Gilbert 109697
Glendale 218812
Glendale 194973
Grand Prairie 127427
Grand Rapids 197800
Green Bay 102313
Greensboro 223891
Hampton 146437
Hartford 121578
Hayward 140030
Henderson 175381
Hialeah 226419
Hollywood 139357
Honolulu 371657
Houston 1953631
Huntington Beach 189594
Huntsville 158216
Independence 113288
Indianapolis 791926
Inglewood 112580
Irvine 143072
Irving 191615
Jackson 184256
Jacksonville 735167
Jersey City 240055
Joliet 106221
Kansas City 441545
Kansas City 146866
Kenosha 89447
Knoxville 173890
Lafayette 110257
Lakewood 144126
Lancaster 118718
Lansing 119128
Laredo 176576
Las Vegas 478434
Lexington-Fayette 260512
Lincoln 225581
Little Rock 183133
Livonia 100545
Long Beach 461522
Los Angeles 3694820
Louisville 256231
Lowell 105167
Lubbock 199564
Macon 113336
Madison 208054
Manchester 107006
McAllen 106414
Memphis 650100
Mesa 396375
Mesquite 124523
Metairie 149428
Miami 362470
Miami Beach 97855
Midland 98293
Milwaukee 596974
Minneapolis 382618
Mission Viejo 98049
Mobile 198915
Modesto 188856
Montgomery 201568
Moreno Valley 142381
Naperville 128358
Nashville-Davidson 569891
New Bedford 94780
New Haven 123626
New Orleans 484674
New York 8008278
Newark 273546
Newport News 180150
Norfolk 234403
Norman 94193
North Las Vegas 115488
Norwalk 103298
Oakland 399484
Oceanside 161029
Odessa 89293
Oklahoma City 506132
Omaha 390007
Ontario 158007
Orange 128821
Orlando 185951
Overland Park 149080
Oxnard 170358
Palmdale 116670
Paradise 124682
Pasadena 141674
Pasadena 133936
Paterson 149222
Pembroke Pines 137427
Peoria 112936
Peoria 108364
Philadelphia 1517550
Phoenix 1321045
Pittsburgh 334563
Plano 222030
Pomona 149473
Portland 529121
Portsmouth 100565
Providence 173618
Provo 105166
Pueblo 102121
Raleigh 276093
Rancho Cucamonga 127743
Reno 180480
Richmond 197790
Richmond 94100
Riverside 255166
Roanoke 93357
Rochester 219773
Rockford 150115
Sacramento 407018
Saint Louis 348189
Saint Paul 287151
Saint Petersburg 248232
Salem 136924
Salinas 151060
Salt Lake City 181743
San Antonio 1144646
San Bernardino 185401
San Buenaventura 100916
San Diego 1223400
San Francisco 776733
San Jose 894943
San Mateo 91799
Sandy 101853
Santa Ana 337977
Santa Clara 102361
Santa Clarita 151088
Santa Monica 91084
Santa Rosa 147595
Savannah 131510
Scottsdale 202705
Seattle 563374
Shreveport 200145
Simi Valley 111351
Sioux Falls 123975
South Bend 107789
Spokane 195629
Springfield 152082
Springfield 151580
Springfield 111454
Stamford 117083
Sterling Heights 124471
Stockton 243771
Sunnyvale 131760
Sunrise Manor 95362
Syracuse 147306
Tacoma 193556
Tallahassee 150624
Tampa 303447
Tempe 158625
Thousand Oaks 117005
Toledo 313619
Topeka 122377
Torrance 137946
Tucson 486699
Tulsa 393049
Vallejo 116760
Vancouver 143560
Virginia Beach 425257
Visalia 91762
Waco 113726
Warren 138247
Washington 572059
Waterbury 107271
West Covina 105080
West Valley City 108896
Westminster 100940
Wichita 344284
Wichita Falls 104197
Winston-Salem 185776
Worcester 172648
Yonkers 196086
name area country_code2 country_code3
Afghanistan 652090.00 AF AFG
Albania 28748.00 AL ALB
Algeria 2381741.00 DZ DZA
American Samoa 199.00 AS ASM
Andorra 468.00 AD AND
Angola 1246700.00 AO AGO
Anguilla 96.00 AI AIA
Antarctica 13120000.00 AQ ATA
Antigua and Barbuda 442.00 AG ATG
$ sqlite3 test.db ## Open sqlite with database name
sqlite> .mode csv
sqlite> .import path_to/cities.csv cities
sqlite> .schema
## verify
CREATE TABLE IF NOT EXISTS "cities"(
"name" TEXT,
"population" TEXT
);
sqlite> select * from cities;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment