Python reads txt text row by line, writes the TXT in line with the split word

Source: Internet
Author: User



Background background:
My TXT file contains search terms, because the original query (search term) is used/or, to divide the word, and I want to achieve is each word is a separate line, and write txt

First step: Read TXT file by line
s = []f  = open (' Querylist.txt ', ' R ') #由于我使用的pycharm已经设置完了路径, so I wrote the file name for lines in F:    # Query_list.append ( Line.replace ('/', '). Replace (', ', '). Replace (","). Strip (' \ n '))    ls = lines.strip (' \ n '). Replace (","). Replace (', ', '/'). Replace ('? ', '). Split ('/') for    i in LS:        s.append (i) F.close () print (s)
Step two: Write txt on a line-by-row basis
Write each element of the s in our list to Tet, an element is a line, there are many ways, I just list A
(Create an empty txt:query_result in your file path in advance.)
F1 = open (' Query_result.txt ', ' W ') for J in S:    f1.write (j+ ' \ n ') F1.close ()

Finally, the result of my output is:

Python reads txt text row by line, writes the TXT in line with the split word

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.