Python Save data to File

Source: Internet
Author: User

1. Print them to screen

Man = []other = []try:    data = open (' sketch.txt ')    for each_line in data:        try:            (role, line_spoken) = Each_li Ne.split (': ', 1)            Line_spoken=line_spoken.strip ()            if role== ' man ':                man.append (line_spoken)            elif role = = ' Other mans ':                other.append (line_spoken)        except ValueError:            pass    data.close () except IOError :    print (' The Datafiel is missing! ') Print (' printing=====man===list======= ', mans) print (' printing=====other===list===== ', other)    

2. Save the data to Man_data.txt and Other_data.txt

Man = []other = []try:    data = open (' sketch.txt ')    for each_line in data:        try:            (role, line_spoken) = Each_li Ne.split (': ', 1)            Line_spoken=line_spoken.strip ()            if role== ' man ':                man.append (line_spoken)            elif role = = ' Other mans ':                other.append (line_spoken)        except ValueError:            pass    data.close () except IOError :    print (' The Datafiel is missing! ') Try:    man_out = open ("Man_data.txt", "W")    other_out = open (' Other_data.txt ', ' a ')    print (' Here is data From list ', Man,file=man_out]    print (' Save man file to Man_data.txt ')    print (' Here's data from list ', Other,file =other_out)    print (' Save other file to Other_data.txt ')    man_out.close ()    other_out.close () except IOError:    print (' Data file is not exist ')

Python Save data to File

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.