Parson #P130. Information stored in csv file

describe

Please save the personally identifiable information in the inf.in file (utf-8 encoding) in csv format to the inf.out file (utf-8 encoding).

Format

Input: inf.in file

姓名:周紫薇
居住地:北京
身份证号:110101199003073597
手机号:19838855767
姓名:广雨旋
居住地:上海
身份证号:310101199003074538
手机号:18334444671
姓名:国天欣
居住地:广州
身份证号:440103199003071398
手机号:17131292567
姓名:高白桃
居住地:深圳
身份证号:440303199003078179
手机号:15725375874

Output: inf.out file

姓名,手机号,身份证号,居住地
周紫薇,19838855767,110101199003073597,北京
广雨旋,18334444671,310101199003074538,上海
国天欣,17131292567,440103199003071398,广州
高白桃,15725375874,440303199003078179,深圳

Code:

fr = open('inf.in', 'r', encoding='utf-8')
fw = open('inf.out', 'w', encoding='utf-8')
fw.write("姓名,手机号,身份证号,居住地\n")
info = {}
count = 0
for line in fr:
    key, value = line.split(":")
    key = key.strip()
    value = value.strip()
    info[key] = value
    count += 1
    if count == 4:
        fw.write(f"{info['姓名']},{info['手机号']},{info['身份证号']},{info['居住地']}\n")
        count = 0
        info = {}
fr.close()
fw.close()

Code analysis:

  1. fr = open('inf.in', 'r', encoding='utf-8'): Open the file named 'inf.in' for reading in read-only mode (. ), and assign the file object to the variable 'r'), use UTF-8 encoding (encoding='utf-8'fr
  2. fw = open('inf.out', 'w', encoding='utf-8'): Open the file named 'inf.out' in write mode ('w'), using UTF-8 encoding (encoding='utf-8'), and assign the file object to the variablefw.
  3. fw.write("姓名,手机号,身份证号,居住地\n"): Write the string "name, mobile phone number, ID number, residence" into the filefw, which means writing the header.
  4. info = {}: Create an empty dictionaryinfo to store the read data.
  5. count = 0: Initialize the countercount to 0, which is used to count the number of rows read.
  6. for line in fr:: Traverse each row of data in the file objectfr and assign each row to the variable line.
  7. key, value = line.split(":"): Use the string's split() method to split each line into two parts by colon ":", and assign the split results to variables < a i=2> and . keyvalue
  8. key = key.strip(): Use the strip() method of string to remove the blank characters at both ends of key, including newline characters.
  9. value = value.strip(): Use the strip() method of string to remove the blank characters at both ends of value, including newline characters.
  10. info[key] = value: Use key as the key of the dictionary info, value as the value of the corresponding key, and store the data in the dictionary info.
  11. count += 1: Countercount increases by 1, indicating that one row of data has been read.
  12. if count == 4:: Ifcount is equal to 4, 4 rows of data (including headers) have been read.
  13. fw.write(f"{info['姓名']},{info['手机号']},{info['身份证号']},{info['居住地']}\n"): Usef-string to write the name, mobile phone number, ID number and place of residence in the dictionaryinfo into a file in comma-separated format a>fw. And add a newline character "\n" at the end.
  14. count = 0: Reset countercount to 0.
  15. info = {}: Clear the dictionaryinfo.

Read the data in the file named 'inf.in' and separate each line by colon into two parts, and use the first part as the key of the dictionary, and the second part as the value of the corresponding key. After reading 4 lines of data, write the name, mobile phone number, ID number and place of residence in a comma-separated format into a file named 'inf.out'.

Guess you like

Origin blog.csdn.net/m0_63501513/article/details/132417388