CSV file converted from geodatabase is red in my script and it keeps cause an error regarding float argument as all the items in the data file are strings. I was seeking a few ways to add argument, but it does not work well.
Could you please provide any suggestion or solution?
Just in case, I upload a part of my script.
import csv
from pulp import *
supplyData = {}
with open('C:\\scripts\\oil_sd.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
supplyData[row['originid']] = [row['oilseed'], 0]
Thank you!
Solved! Go to Solution.
Please test your script using the top 5 lines of your CSV file, if it does not work, then please add these few lines here for us to solve!
(We think it may be your CSV data, for example you may be trying to save a "String" inside a "Number" field?)
What exactly is the error? This seems to work for me with my own csv.
I got this error. "TypeError: float argument required, not str"
Yes, the script that I attached has no problem itself. I believe that the data imported from CSV used in a linear programming calculation could cause the issue. And I also wonder your csvfile has texts...
I upload all the entire script just for clarifying my issue.
import csv
from pulp import *
supplyData = {}
with open('C:\\scripts\\oil_sd.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
supplyData[row['originid']] = [row['oilseed'], 0]
demandData = {}
with open('C:\\scripts\\fu_stn.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
demandData[row['destinationid'])] = [0, row['oilseed']]
nodeData = {}
nodeData = dict(supplyData.items() + demandData.items())
Nodes = [(b) for b in nodeData.keys()]
arcData = {}
with open('C:\\scripts\\route.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
arcData[row["originid"], row["destinationid"]]=[row['cost'], row['min'], row['max']]
Arcs =[(k) for k in arcData.keys()]
(supply, demand) = splitDict(nodeData)
(costs, mins, maxs) = splitDict(arcData)
vars = LpVariable.dicts("Route",Arcs,None,None,LpInteger)
for a in Arcs:
vars.bounds(mins, maxs)
prob = LpProblem("Supply Chain Problem",LpMinimize)
prob += lpSum([vars* int(costs) for a in Arcs]), "Total Cost of Transport"
prob.writeLP("OilseedSupplyChain.lp")
prob.solve()
print("Status:", LPStatus[prob.status])
for v in prob.variables():
print(v.name, "=", v.varValue)
print("Total Cost of Transportation = ", value(prob.objective))
Of course a few lines of the csv file would provide a nice context to assess your script.
I uploaded my entire script as the reply for the first comment. I wish it can clarify the issue of my script.
Thank you.
Please test your script using the top 5 lines of your CSV file, if it does not work, then please add these few lines here for us to solve!
(We think it may be your CSV data, for example you may be trying to save a "String" inside a "Number" field?)