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:
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
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
.fw.write("姓名,手机号,身份证号,居住地\n")
: Write the string "name, mobile phone number, ID number, residence" into the filefw
, which means writing the header.info = {}
: Create an empty dictionaryinfo
to store the read data.count = 0
: Initialize the countercount
to 0, which is used to count the number of rows read.for line in fr:
: Traverse each row of data in the file objectfr
and assign each row to the variableline
.key, value = line.split(":")
: Use the string'ssplit()
method to split each line into two parts by colon ":", and assign the split results to variables < a i=2> and .key
value
key = key.strip()
: Use thestrip()
method of string to remove the blank characters at both ends ofkey
, including newline characters.value = value.strip()
: Use thestrip()
method of string to remove the blank characters at both ends ofvalue
, including newline characters.info[key] = value
: Usekey
as the key of the dictionaryinfo
,value
as the value of the corresponding key, and store the data in the dictionaryinfo
.count += 1
: Countercount
increases by 1, indicating that one row of data has been read.if count == 4:
: Ifcount
is equal to 4, 4 rows of data (including headers) have been read.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.count = 0
: Reset countercount
to 0.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'.