简体   繁体   中英

Why does "\n" appear in my string output?

I have elements that I've scraped off of a website and when I print them using the following code, they show up neatly as spaced out elements.

print("\n" + time_element) 

prints like this

F
4pm-5:50pm

but when I pass time_element into a dataframe as a column and convert it to a string, the output looks like this

 # b' \n F\n \n 4pm-5:50pm\n

I am having trouble understanding why it appears so and how to get rid of this "\n" character. I tried using regex to match the "F" and the "4pm-5:50pm" and I thought this way I could separate out the data I need. But using various methods including

# Define the list and the regex pattern to match time = df['Time'] pattern = '[AZ]+' # Filter out all elements that match the pattern filtered = [x for x in time if re.match(pattern, x)] print(filtered)

I get back an empty list.

From my research, I understand the "\n" represents a new line and that there might be invisible characters. However, I'm not understanding more about how they behave so I can get rid of them/around them to extract the data that I need.

When I pass the data to csv format, it prints like this all in one cell

F
4pm-5:50pm

but I still end up in the similar place when it comes to separating out the data that I need.

you can use the function strip() when you extract data from the website to avoid "\n"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM