Get the App
SLTechnology News&Howtos  ›  Development  › 

What if there is a blank line in the python read file?

Shulou Source: shulou.com Published: 2022-06-03 15:34:07 09月15日 Update

Editor to share with you what to do when there is a blank line in the python reading file, I believe most people do not know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's go to know it together.

Problem description: file tesxt.txt content, read the file by line, print to the console, there are multiple blank line problems.

Because print () adds line breaks by default when printing, so add.

1. If there is a blank line in the output of the file, you can simply cancel the newline of print.

2. Add an end= "" parameter that cancels line wrapping to print (): print (line,end= "").

Has nothing to do with windows and unix.

Example

# Code 1with open ('test.txt','r') as f: lines = f.readlines () for line in lines: print (line,end= ") f.close () # Code 2.f = open (" test.txt "," rt ") # many people on the Internet say rU or rt. Content = f.readlines () for i in content: print (iEnd = ") f.close () = ccccccccccd888888888ddddddddddddddddd00333343434343fsddfsafsdfasdfsadfsadsfccccccccccd888888888ddddddddddddddddd00333343434343fsddfsafsdfasdfsadfsadsf is all the contents of the article" what to do if there is a blank line in the python read file ". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Documents blank lines content articles how to do code problems not much parameters multiple most instances console more knowledge industry information channels channels references Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Technology Shulou Information Linux macOS