Convert multiple lines of text files to a list and parse them into a dictionary.

Source: Internet
Author: User

Convert multiple lines of text files to a list and parse them into a dictionary.

# Text content Rain 80000 \ n Egon 50000 \ n Yuan 30000 \ nsalary = open ("salaryinfo.txt", "r +", encoding = "UTF-8 ") salary_list = [] # defines a new list value. For line in salary. readlines (): salary_list.append (list (line. split () # The list read is appended to the salary_list value. Print (salary_list) print ("--- I am a separation line ---") salary_dict ={}# define a new dictionary value for dic in salary_list: # circular dictionary salary_dict [dic [0] = int (dic [-1]) # The left and right sides are the K of the dictionary, and the right side is the V of the dictionary, because the content of the original text is a string that is not an integer, here is an int type. # Output result! [['Alex ', '123'], ['rain', '123'], ['egon', '123'], ['yuany ', '20140901'] --- I am a separator ---- {'Alex ': 30000, 'rain': 100000, 'egon': 80000, 'yuanyuan': 50000}

 



 

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.