Need help: Thread Shed output shows \n instead of new line


#1

daily_sales_replaced = daily_sales.replace(";,;", “@”)
daily_transactions = daily_sales_replaced.split("@")

daily_transactions_split = []
for i in daily_transactions:
daily_transactions_split.append(i.split(","))
transactions_clean = []
for lst in daily_transactions_split:
for sublst in lst:
transactions_clean.append(sublst.strip(" "))
print(transactions_clean)

will display [‘Edith Mcbride’, ‘$1.21’, ‘white’, ‘\n09/15/17’

What am I doing wrong?

https://www.codecademy.com/paths/computer-science/tracks/cspath-python-objects/modules/cspath-python-strings/projects/thread-shed


#2

Context / details provided is not enough for anyone to give insights on the situation


#4

alright, i’m gonna past the whole code and see if that helps:

daily_sales = \
"""Edith Mcbride   ;,;$1.21   ;,;   white ;,; 
09/15/17   ,Herbert Tran   ;,;   $7.29;,; 
white&blue;,;   09/15/17 ,Paul Clarke ;,;$12.52 
;,;   white&blue ;,; 09/15/17 ,Lucille Caldwell   
;,;   $5.13   ;,; white   ;,; 09/15/17,"""

daily_sales_replaced = daily_sales.replace(";,;", "@")
daily_transactions = daily_sales_replaced.split("@")

daily_transactions_split = []
for i in daily_transactions:
	daily_transactions_split.append(i.split(","))
transactions_clean = []
for lst in daily_transactions_split:
	for sublst in lst:
		transactions_clean.append(sublst.strip(" "))
print(transactionsclean)

The list I get that gets printed is:

['Edith Mcbride', '$1.21', 'white', '\n09/15/17', 'Herbert Tran', '$7.29', '\nwhite&blue', '09/15/17', 'Paul Clarke', '$12.52 \n', 'white&blue', '09/15/17', 'Lucille Caldwell   \n', '$5.13', 'white', '09/15/17', '']

You see those \n that gets printed? ‘\n09/15/17’, ‘\nwhite&blue’, etc…
I need them removed.
Thanks!


#5

So remove them. You already know how.


Python supports method chaining.

object.method.method.method

Knowing how greedy the replace method is and into the mix, method chaining…

x.replace('x','y').replace('a', 'b').replace('p','f')

#7