CSV Import From European Country

A multinational client needs to be able to import CSV files via an Ignition project. All works well when the CSV is created (via Excel) and imported in the US. The issues occur when the CSV is created and imported in Europe. Following is the code I am using to open the file:

if system.util.getLocale()[:2] == 'en':
	csv_data = csv.reader(open(path, 'rb'))
	csv_data = csv.reader(open(path, 'rb'), delimiter=';')

Any assistance with the following questions is greatly appreciated:

  1. Is there a preferred method for dealing with commas used as decimal separators in floating point numbers?
  2. Is there a better method for opening the file?


Replace them. Even Ignition has (or had) bugs with commas vs points. We have a few projects where any keyboard input event insterting a comma is replaced by a point. As for most projects, they don’t need to type long texts, as long as the numbers are correct, it’s all we need. We didn’t like a motor getting set to 55 Hz when the operator meant 5.5 Hz.

Not really, CSV is very unstandardised. So writing universal code for it is almost impossible. The only thing you can do is try to get to know the special cases (how it’s quoted, what delimitors are used, what number and date format, …) and read the file as text and parse it yourself.

should the 2nd line of your code be:

csv_data = csv.reader(open(path, 'en'))

instead of:

csv_data = csv.reader(open(path, 'rb'))

‘en’ is not a valid mode for the open function.

If they’re separated by semi-colons, wouldn’t that make them SCSVs? :grin:

1 Like