import csv
open the pipe-delimited text file, select the rows with FEATURE_CLASS "Falls" and append FEATURE_NAME, STATE_ALPHA, and COUNTY_NAME to the variable gnisList, then print it
gnisList = []
with open("C:/Users/zina/Documents/school/GIS_GEO409/GIS_work/Data/NationalFile_20181001.txt", encoding='utf-8') as csvfile:
reader = csv.reader(csvfile,delimiter="|")
for row in reader:
if row[2] == "Falls":
gnisList.append(row[1])
gnisList.append(row[3])
gnisList.append(row[5])
# this is a very long list, may not want to print it
# print(gnisList)
open the pipe-delimited file, and read into the variable fallList those rows with FEATURE_CLASS "Falls" and STATE_ALPHA "KY", then print it
#import csv
fallList = []
with open("C:/Users/zina/Documents/school/GIS_GEO409/GIS_work/Data/NationalFile_20181001.txt", encoding='utf-8') as csvfile:
reader = csv.reader(csvfile,delimiter="|")
for row in reader:
if row[2] == "Falls" and row[3] == "KY":
fallList.append(row[1])
fallList.append(row[-4])
print(fallList)
# import csv
# fall2List = []
# with open("C:/Users/zina/Documents/school/GIS_GEO409/GIS_work/Data/NationalFile_20181001.txt", encoding='utf-8', mode=w) as fallsfile:
# fallswriter = csv.writer(fallsfile,delimiter="|")
# for row in reader:
# if row[2] == "Falls" and row[3] == "KY":
# fall2List.append(row)
# print(fall2List)
using pandas can format file so it is easier to read, using the DataFrame
import pandas as pd
import numpy as np
with open("C:/Users/zina/Documents/school/GIS_GEO409/GIS_work/Data/NationalFile_20181001.txt", encoding='utf-8') as csvfile:
reader = pd.read_csv(csvfile,delimiter="|")
pdData = pd.DataFrame(reader)
select rows in which FEATURE_CLASS is "Falls" and STATE_ALPHA is "KY" and only include the listed fields
#pdData[["FEATURE_NAME","FEATURE_CLASS", "ELEV_IN_FT"]].sort_values(by=["ELEV_IN_FT"])
pdData[(pdData["FEATURE_CLASS"] == "Falls") & (pdData["STATE_ALPHA"] == "KY")][["FEATURE_ID","FEATURE_NAME","FEATURE_CLASS","STATE_ALPHA","COUNTY_NAME","PRIMARY_LAT_DMS","PRIM_LONG_DMS","PRIM_LAT_DEC","PRIM_LONG_DEC","ELEV_IN_FT"]].sort_values(by=["ELEV_IN_FT"])
Use the to_csv() pandas method to save to CSV
pdData[pdData["FEATURE_CLASS"] == "Falls"].to_csv("../Data/falls_us.csv")
pdData[(pdData["FEATURE_CLASS"] == "Falls") & (pdData["STATE_ALPHA"] == "KY")].to_csv("../Data/falls_ky.csv")