Home > Back-end >  Python string.replace, only replace under certain circumstances
Python string.replace, only replace under certain circumstances

Time:05-12

I'm analyzing sales data I got from receipts. All bought items are in one column as one string like this:

"1 x Sandwich, "2 x Coffee, with cream", 1 x Apple pie"

I wish to separate all items to calculate the amount of items bought. A simple string.split(',') won't do, since there are also commas in the names of certain items. Luckily, these names are encapsulated by double quotes and 'normal' names are not.

How can I replace the commas within double quotes and not the commas separating items?

If these commas in names change into colons, for example, parsing the string can be done with string.split(). So the desired output will be something like this:

"1 x Sandwich, "2 x Coffee: with cream", 1 x Apple pie"

There might be other solutions, but this problem got me thinking about replacing very specific characters.

CodePudding user response:

text = '1 x Sandwich, "2 x Coffee, with cream", 1 x Apple pie'

def comma_changer(text):
  text = list(text)
  quote_counter = 0
  for i,char in enumerate(text):
    if char == '"':
      quote_counter =1
    elif char == ",":
      if quote_counter%2 == 1:
        text[i] = ":"
  return("".join(text))

comma_changer(text) #'1 x Sandwich, "2 x Coffee: with cream", 1 x Apple pie'

CodePudding user response:

you need to try to tell it to separate it by a specific character. in this case, try string.split('"')

CodePudding user response:

Your input is invalid because of one missing closing " and one missing opening ".

"1 x Sandwich, "2 x Coffee, with cream", 1 x Apple pie"
             ^                           ^

I am using Pythons csv module here. Very important is the option skipinitialspace because you have blank chars (space) after your , which is unusual in CSV files.

#!/usr/bin/env python3
import io
import csv

your_invalid_input = '"1 x Sandwich, "2 x Coffee, with cream", 1 x Apple pie"'
valid_input        = '"1 x Sandwich", "2 x Coffee, with cream", "1 x Apple pie"'

# this simulates a file object
raw_data = io.StringIO(valid_input)

csv_reader = csv.reader(raw_data,
                        delimiter=',',
                        skipinitialspace=True)

for line in csv_reader:
    print(line)

The output is

['Sandwich', ' "Coffee', ' with cream"', ' "Coffee', ' with cream"', ' "Apple pie"']
  • Related