\n Line drop does not apply to variables saved as files, and "Add" to variables like this will result in line drop. It's the same content, but I wonder why the rope down is not applied.

Asked 6 days ago, Updated 6 days ago, 2 views

search_data = "./Middle school 1 math to rational numbers.html"

f = open(search_data',rt',encoding='utf-8') #Openfile with 'UTF-8' encoding

content = f.readlines()

f.close()

print(content[11])

temp = content[11]

The contents of the file are: I'd like to make a program where the contents below are written in one line and you can get them one by one when you click. However, if you receive it in a file and print it out in a content array for each line, \n characters appear as they are.

temp="Addition and subtraction of integers\n 정Addition of integers\n·Addition of two integers with the same sign: Put a common sign on the sum of the absolute values of the two integers.\n·Addition of two different integers: Sign the difference between the absolute values of the two integers with a larger absolute value.\n② Subtraction of integers: Change the sign of the subtractive number and calculate it by addition.\n"

If you do this, \n will print out well. What's the reason? And how can I express the printing method that I want?

Addition and subtraction of integers\n 정Addition of integers\n·Addition of two integers with the same sign: Put a common sign on the sum of the absolute values of the two integers.\n·Addition of two different integers: Sign the difference between the absolute values of the two integers with a larger absolute value.\n 정수 Integer Subtraction of: The subtraction is calculated by changing the sign of the subtraction to addition.\n


2022-09-20 15:17

2 Answers

This is because the readlines() function saves one more \ to store special characters in the line as they are. All you have to do is remove it

temp = content[0].replace('\\n', '\n')
print(temp)


2022-09-20 15:17

It's working well. Thank you


2022-09-20 15:17

If you have any answers or tips


© 2022 pinfo. All rights reserved.