I want to load the data given in the URL "https://www.treasury.gov/ofac/downloads/sdn.csv" directly into a table named sdn. The only change I want to do is to replace all the '-0- ' with '' for all the columns that have this value.
I tried to do this using pandas but the approach I have does not look clean.
import requests
import pandas as pd
sdnURL = "https://www.treasury.gov/ofac/downloads/sdn.csv"
altURL = "https://www.treasury.gov/ofac/downloads/alt.csv"
addURL = "https://www.treasury.gov/ofac/downloads/add.csv"
sdnCommentsURL = "https://www.treasury.gov/ofac/downloads/sdn_comments.csv"
sdnHeader = ["sdn_id", "sdn_name", "sdn_type", "program", "title", "call_sign", "vessel_type", "tonnage", "gross_tonnage", "vessel_flag", "vessel_owner", "remarks"]
altHeader = ["sdn_id", "alt_id", "alt_type", "alt_name", "remarks"]
addHeader = ["sdn_id", "address_id", "address", "city_state_post", "country", "remarks"]
sdnCommentsHeader = ["sdn_id", "remarks"]
sdn = pd.read_csv(sdnURL, names = sdnHeader, header = None)
alt = pd.read_csv(altURL, names = altHeader, header = None)
add = pd.read_csv(addURL, names = addHeader, header = None)
sdnComments = pd.read_csv(sdnCommentsURL, names = sdnCommentsHeader, header = None)
sdn.to_csv('sdn.csv', index = False)
alt.to_csv('alt.csv', index = False)
add.to_csv('add.csv', index = False)
sdnComments.to_csv('sdnComments.csv', index = False)
Further I plan to load csv to the mysql table. I have two concerns in my approach -
- I don't want to write the commands for each file.
- Replacing the '-0- ' from all the columns in one go
Final Edit: Thanks @Jimmar for the answer, I finally ended up writing the code like this -
import requests
import pandas as pd
files = {
"sdn" : ["sdn_id", "sdn_name", "sdn_type", "program", "title", "call_sign", "vessel_type", "tonnage", "gross_tonnage", "vessel_flag", "vessel_owner", "remarks"],
"alt" : ["sdn_id", "alt_id", "alt_type", "alt_name", "remarks"],
"add" : ["sdn_id", "address_id", "address", "city_state_post", "country", "remarks"],
"sdn_comments" : ["sdn_id", "remarks"]
}
def fetch_csv(file, headers):
df = pd.read_csv("https://www.treasury.gov/ofac/downloads/"+file+".csv", names=headers, header=None)
df = df.replace('-0- ', '')
df.to_csv(file+'.csv', index=False)
for file, headers in files.items():
fetch_csv(file, headers)