1

So this should be super easy but I'm struggling for a while now: I learn python at moment and there is this Task: I have string with data, sales data. Name of customer, price of goods etc...

daily_sales = """Leticia Manning;,;$15.70 ;,; green&purple;,; 
09/15/17 ,   Mario Wallace ;,; $12.36 ;,;green ;,; 
09/15/17,Lewis Glover;,;   $13.66   ;,;   
green&white;,;09/15/17,   Gail Phelps   ;,;$30.52   
;,; green&white&blue   ;,; 09/15/17 , Myrtle Morris 
;,;   $22.66   ;,; green&white&blue;,;09/15/17"""

I want to sort it in to list so it's readable.

def sort(daily_sales):
    sales = daily_sales.replace(";,;", "|")
    sales_list = sales.split(",")
    transactions = []
    for objects in sales_list:
        transactions.append(objects.replace(" ", ""))
    return transactions

print(sort(daily_sales))

So far so easy. This is my output:

['LeticiaManning|$15.70|green&purple|\n09/15/17',
 'MarioWallace|$12.36|green|\n09/15/17',
 'LewisGlover|$13.66|\ngreen&white|09/15/17',
 'GailPhelps|$30.52\n|green&white&blue|09/15/17',
 'MyrtleMorris\n|$22.66|green&white&blue|09/15/17']

But i want in front of and after the parting line one empty space. Like:

'LeticiaManning | $15.70 | green&purple | \n09/15/17'

But i have no idea how to implement it. I tried with .join but obviously it doesn't work. I thought about an interleaved for-loop but i have no idea how this could work.

4
  • 2
    in the replace change '|' to ' | ' Commented Feb 23, 2020 at 20:36
  • That wouldn't work as you remove all spaces in the end. Commented Feb 23, 2020 at 20:36
  • Then do it afterwards (before print(sorted(...))) Commented Feb 23, 2020 at 20:38
  • You can also remove all the spaces first. Commented Feb 23, 2020 at 20:43

4 Answers 4

2

Since you get rid of all spaces in this line:

transactions.append(objects.replace(" ", ""))

you must add them after:

transactions.append(objects.replace(" ", "").replace("|", " | "))
Sign up to request clarification or add additional context in comments.

Comments

1

To add a space before and after each |, you can add

t = t.replace("|"," | ")

to each transaction.

The full code would look like this:


def sort(daily_sales):
    sales = daily_sales.replace(";,;", "|")
    sales_list = sales.split(",")
    transactions = []
    for objects in sales_list:
        transactions.append(objects.replace(" ", ""))
        transactions[-1] = transactions[-1].replace("|", " | ")
        # add spaces to the new transaction after creating it
    return transactions

print(sort(daily_sales))

Comments

0

Remove spaces first, then you can insert them after. As a bonus, this saves having to iterate through sales_list.

def sort(daily_sales):
    sales = daily_sales.replace(" ", "")
    sales = sales.replace(";,;", " | ")
    sales_list = sales.split(",")
    return sales_list

for s in sort(daily_sales):
    print(repr(s))

Output:

'LeticiaManning | $15.70 | green&purple | \n09/15/17'
'MarioWallace | $12.36 | green | \n09/15/17'
'LewisGlover | $13.66 | \ngreen&white | 09/15/17'
'GailPhelps | $30.52\n | green&white&blue | 09/15/17'
'MyrtleMorris\n | $22.66 | green&white&blue | 09/15/17'

2 Comments

works ! i searched for repr() but didn't find a clear definition. what does it ?
@sanik It returns a string representation of a Python object. See docs: repr
0

Hi and welcome to Stack Overflow, You can also use re to add space before and after | character
something like this :

import re

daily_sales=['LeticiaManning|$15.70|green&purple|\n09/15/17',
 'MarioWallace|$12.36|green|\n09/15/17',
 'LewisGlover|$13.66|\ngreen&white|09/15/17',
 'GailPhelps|$30.52\n|green&white&blue|09/15/17',
 'MyrtleMorris\n|$22.66|green&white&blue|09/15/17']

data = ''.join(daily_sales)

pattern = re.compile(r"([|])")
print (pattern.sub(" \\1 ", data))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.