import requests
import os
import os.path
import sys
import importlib
import pandas as pd
if os.path.isdir(os.path.join("../../..", "modules")):
module_dir = os.path.join("../../..", "modules")
else:
module_dir = os.path.join("../..", "modules")
module_path = os.path.abspath(module_dir)
if not module_path in sys.path:
sys.path.append(module_path)
import mysocket as sock
importlib.reload(sock)
import util
importlib.reload(util)
datadir = "publicdata"
import io
s = """year,sex,name,count
2014,Female,Emma,20936
2014,Male,Noah,19305
2015,Female,Emma,20455
2015,Male,Noah,19635
2016,Female,Emma,19496
2016,Male,Noah,19117
2017,Female,Emma,19800
2017,Male,Liam,18798
2018,Female,Emma,18688
2018,Male,Liam,19837
"""
print(s)
print(repr(s))
pathname = os.path.join(datadir, "tn10.csv")
fileObj = open(pathname, "rt")
columns = fileObj.readline().strip().split(',')
print(columns)
LoL = []
for row in fileObj:
fields = row.strip().split(',')
LoL.append(fields)
print(LoL)
fileObj.close()
df = pd.DataFrame(LoL, columns=columns)
df
read_csv
¶df2 = pd.read_csv(pathname)
with open(pathname, 'rt') as fileObj:
df3 = pd.read_csv(fileObj)
df3
# df4 = pd.read_csv(s)
Goal: Given data is in a string, how to create two-D structure or data frame
What to do with a string
csvurl = util.buildURL("/data/ind2016_16.csv", "datasystems.denison.edu")
response = requests.get(csvurl)
if response.status_code != 200:
print("Error acquiring file")
response.text
How to make "right"