Menu

Normalize decimal-comma prices in a CSV before a Shopify import (Python, stdlib only)
📰

Normalize decimal-comma prices in a CSV before a Shopify import (Python, stdlib only)

DEV Community: python·BotHot·12 days ago
#sxSouBd9
#devtopython#devto#dev#column#replace
Long article (563 words) — no AI summary yet.
Reading 0:00
15s threshold

Product exports from German or French spreadsheets often contain prices like 12,90 or 1.299,00 € . Shopify expects 12.90 and 1299.00 , so an import with the wrong format fails or gives wrong prices. Here is a small script that normalizes one price column and tells you which rows it could not parse.

The rules

Remove spaces, non-breaking spaces and currency symbols.

If both , and . appear, the last one is the decimal separator.

If only a comma appears, it is the decimal separator.

Anything that is not digits with at most two decimals afterwards is reported, not guessed.

The script

import csv , re , sys

def normalize ( value ) : v = value . strip ( ) . replace ( "\u00a0" , "" ) . replace ( " " , "" ) v = re . sub ( r "[€$£]|EUR|USD" , "" , v ) if not v : return None if "," in v and "." in v : if v . rfind ( "," ) > v . rfind ( "." ) : v = v . replace ( "." , "" ) . replace ( "," , "." ) else : v = v . replace ( "," , "" ) elif "," in v : v = v . replace ( "," , "." ) if not re .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.