In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how Python deletes the line number of each line in a file. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. What
This line number is really annoying. Try to write a py to remove it.
two。 Train of thought
Def second_of_str segmentation, take the element to the right of the delimiter and return a list
Def move passes in the file path, reads each line, stores the list, and calls the split second_of_str to save the new list.
The main function calls move
3. Code def second_of_str (str,splitsymbol): s = str.split (splitsymbol,1) # element # if len (s) = = 1: # return [] return s [1] def move (file_path, new_file_path): with open (file_path) Encoding='utf-8') as f: content = f.read (). Splitlines () # for line in content: # print (line) new_content = [] for line in content: new_line = second_of_str (line '.) # add a new list new_content.append (new_line) for line in new_content: print (line) F1 = open (new_file_path,encoding='utf-8') for each line after removal Mode ='w') for line in new_content: f1.writelines (line) f1.writelines (["\ n"]) f1.close () return new_contentif _ _ name__ = ='_ _ main__': f = move ('1.txt`)
Finished, the result ~
Thank you for reading! This is the end of this article on "how to delete the line number of each line of a file by Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.