Python 3 course: Strings section: Thread Shed project

Hello everyone!

My question concerns the following linked project:

https://www.codecademy.com/courses/learn-python-3/projects/thread-shed

My problem originates at step 3, after only the following 3 lines of code:

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

For some reason, the .split() function is adding many ‘\n’ into my output that don’t exist in the data prior to that operation. I’ve corroborated my code with the code provided in the Codecademy companion video, and it seems to be the same. I continued in spite of the extra newlines until step 14, when I’m supposed to sum the values included in the Sales list, but because of the many ‘\n’ I’m unable to convert a lot of these numeric strings into actual float values. Does anyone have guidance on why the ‘\n’ are being added by the .split() function in step 3? Barring that, does anyone have advice for how to perform a built-in string function to remove the newlines from my data output?

Thanks much! (My first time posting in these forums, so apologies if my question is formatted/documented incorrectly!)
-Justin

The new line characters are expected. Did you perhaps skip over instruction #8 where you use .strip()?

1 Like

Thanks midlindner! That solved my problem :slight_smile:

2 Likes

Hey guys

Maybe you could take a minute and check my code? Much appreciated!

I had the same-ish issue as friskybunnies1, but in my case the “\n” got striped automatically or at least in a way I didn’t understand. :wink:

Here’s the relevant part of my code, it’s basically what Matt proposes in the video.

print("STEP 4")
transactions_clean = []
for transaction in daily_transactions_split:
  single_transaction = []
  for element in transaction:
    single_transaction.append(element.strip())
  transactions_clean.append(single_transaction)
print(transactions_clean)

I checked the Get unstuck video and he adds a .replace method:

single_transaction.append(element.replace("\n", “”).strip())

But for some reason, my code doesn’t need this, as show the result of my terminal:

STEP 4
[[‘Gail Phelps’, ‘$30.52’, ‘green&white&blue’, ‘09/15/17’], [‘Myrtle Morris’, ‘$22.66’, ‘green&white&blue’, ‘09/15/17’]]

I found the reason to this: .strip cleans an element not only of whitespace but also of \n. Don’t think, this has been mentioned anywhere in the course so far.

Pretty sure at some point the meaning of ‘white space’ is given to include the newline character.

Thank you, mtf, for your answer. Well possible I missed or forgot it. Finding answers outside of codeacademy is an important skill, too. :slight_smile: Have a great day!

1 Like

my code is only giving a list of empty lists. I looked at the tutorial and I don’t see where I’m going wrong. Can someone explain this to me?

#stripping white space
transcations_clean =
for transaction in daily_transactions_split:
transaction_clean =
for data in transaction_clean:
transaction_clean.append(data.strip())
transcations_clean.append(transaction_clean)

Here is what is printed to the console:

[, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ]

My previous code printed out the daily_transactions_split the same way as the turotrial so I’m lost as to what is happening

image

Double check what your loops are iterating through, especially for wee typos like missing an s. For the best option for posting code to the forums see- How do I format code in my posts? :slightly_smiling_face:.