I can tell you how to get the bugs out, but you'd have to cook them right away (or can them). One bit of advice, do not open them in your house again. They will get out and infest everything in your house and I'm not joking. I lost over 500 lbs of grain foods from doing that - flour, meal, crackers, noodles, all sorts of things.

To get the bugs out, pour them into a colander outside, shake it well to get out as many bugs as possible, then dump the beans into a pot of water. Do this for all of them. Then start scooping out any bugs that float to the top of the water. Keep doing that until you no longer find bugs. Now soak them overnight and scoop any additional bugs that float up. Now drain the beans, rinse them, and either can or cook them. If you cook them, you don't have to eat them all right now, you can freeze them in meal-size portions.

I know that sounds gross, but, if it comes down to getting the bugs out of something or going hungry, I'm getting the bugs out. Might as well learn how now.

As far as preventing bugs in foods, the only sure-fire way I know of is to store them in the freezer all the time.

I know I didn't offer much hope, but that's all I've got.