I was told that I could use Python at my company, so as a memorandum for CSV data processing.
infile.csv
number,name,birthymd,sex,yubin,adr1,adr2,sinsei_ymd,nintei_ymd,happu_ymd,type,pet,test_nowymd
0000111111,Taichiro Ando,19860602,1,1231111,AAA Prefecture BBB City CCC-123,Sample Villa A Room 001,20190501,20190507,20190507,1,0,20200229
0000222222,Ito Sumijiro,19920505,1,1232222,AAA Prefecture BBB City CCC-123,Sample Villa B Room 002,20190501,20190514,20190514,2,1,20200228
0000333333,Cormorant fishing,19990909,2,1233333,AAA Prefecture BBB City CCC-123,Sample Villa C Room 003,20190501,20190514,20190514,1,2,20200131
0000444444,Koshiro Esaki,19870831,1,1234444,AAA Prefecture BBB City CCC-123,Sample Villa D Room 004,20190501,20190507,20190507,2,0,20191231
0000555555,Okamoto Kogoro,19520205,1,1235555,AAA Prefecture BBB City CCC-123,Sample Villa E Room 005,20190601,20190615,20190615,1,2,20191130
0000666666,Meiji Kakizaki,19811030,1,1236666,AAA Prefecture BBB City CCC-123,Sample Villa F Room 006,20190601,20190608,20190608,1,2,20191129
0000777777,Jojima Kanako,19721103,2,1237777,AAA Prefecture BBB City CCC-123,Sample Villa G 007 Room,20190601,20190608,20190608,2,1,20191030
0000888888,Kugimiya Ayabu,19330303,2,1238888,AAA Prefecture BBB City CCC-123,Sample Villa H 008 Room,20190501,20190507,20190507,1,1,20190401
0000999999,Keido Kankuro,19680229,1,1239999,AAA Prefecture BBB City CCC-123,Sample Villa I Room 009,20190601,20190615,20190615,2,0,20190331
1111000000,Koyanagi Sarujuro,19200222,1,1240000,AAA Prefecture BBB City CCC-123,Sample Villa J Room 010,20190501,20190507,20190507,2,1,20190228
1111111111,Juichiro Sado,19430130,1,1241111,AAA Prefecture BBB City CCC-123,Sample Villa K 011 Room,20190601,20190608,20190608,1,0,20190227
1111222222,Shishido Jujiro,19530301,1,1242222,AAA Prefecture BBB City CCC-123,Sample Villa L 012,20190501,20190507,20190507,2,1,20190131
csv_in_out.py
#Variable initialization
##Variables for counters
[line_count, person_count] = [0, 0]
##Variables related to input values
[number, name, birthymd, sex, post, adr1, adr2, ymd_sinsei, ymd_nintei, ymd_happu, types, pet, ymd_test] = ['', '', '', '', '', '', '', '', '', '', '', '', '']
##Variable 1 related to output value
[birth_year, birth_month, birth_day, infile, outfile, line, item_str, data_str] = [0, 0, 0, '', '', '', '', '']
##Variable 2 related to output value (empty array)
item = set()
data = set()
#When opening when specifying the line feed code of the file.
## \n(LF): Unix-like OS in general, Mac OS X, \r\n(CR+LF): Windows OS, \r(CR): Old Mac OS (9 and earlier)
with open('infile.csv', 'r', encoding='utf-8', newline='\n') as infile, \
open('outfile.csv', 'w', encoding='utf-8', newline='\n') as outfile:
#Read and process the contents of infile line by line
for line in infile:
#Map the items of the read line as a character string to the corresponding variables in order from the beginning, separated by commas.
number, name, birthymd, sex, post, adr1, adr2, ymd_sinsei, ymd_nintei, ymd_happu, types, pet, ymd_test \
= map(str, line.split(','))
#Read CSV line count +1
line_count += 1
#The first line of the read CSV line count is the item name. The actual data is from the second line.
#Console output example for confirmation 1: ○ Eye processing
if line_count >= 2:
person_count += 1
#Console output
print(u'{}We are processing the eyes....'.format(person_count))
#Console output example for confirmation 2: Display the date of birth by zero suppression
if line_count >= 2:
birth_year = int(birthymd[0:4])
birth_month = int(birthymd[4:6])
birth_day = int(birthymd[6:8])
print(u'Date of birth{}/{}/{}is.'.format(birth_year,birth_month,birth_day))
#Console output example for confirmation 3: Display number, name, date of birth as a set
if line_count >= 2:
#Console output (edit string)
print(u'number{}of{}Is{}/{}/{}birth.'.format(number,name,birth_year,birth_month,birth_day))
#The main processing part for output.
##After specifying the item name and actual data you want to output in an array, change it to a comma-delimited character string and print it to outfile.
if line_count == 1:
#File output (comma-separated character string: item name)
item = ["number", "Full name", "Birthday", "sex", "郵便number", "Address 1", "Address 2", "Application date", "Certification date", "Date of promulgation", "Type", "Pets", "Test date"]
item_str = ",".join(map(str, item))
print(item_str, file=outfile)
if line_count >= 2:
#File output (comma-separated character string: actual data)
# < !If you want to process the data or count it, write the logic here.! >
data = [number, name, birthymd, sex, post, adr1, adr2, ymd_sinsei, ymd_nintei, ymd_happu, types, pet, ymd_test]
data_str = ",".join(map(str, data))
print(data_str, file=outfile, end='')
#Console output example for confirmation 4: Display output data
if line_count >= 2:
print(u'file output >> {}'.format(data_str),end='')
#Console output example 5 for confirmation: Display of the total number of processes
print(u'') #new line
print(u'total{}Processed the matter.'.format(person_count))
outfile.csv
number,Full name,Birthday,sex,郵便number,Address 1,Address 2,Application date,Certification date,Date of promulgation,Type,Pets,Test date
0000111111,Taichiro Ando,19860602,1,1231111,AAA Prefecture BBB City CCC-123,Sample Villa A Room 001,20190501,20190507,20190507,1,0,20200229
0000222222,Ito Sumijiro,19920505,1,1232222,AAA Prefecture BBB City CCC-123,Sample Villa B Room 002,20190501,20190514,20190514,2,1,20200228
0000333333,Cormorant fishing,19990909,2,1233333,AAA Prefecture BBB City CCC-123,Sample Villa C Room 003,20190501,20190514,20190514,1,2,20200131
0000444444,Koshiro Esaki,19870831,1,1234444,AAA Prefecture BBB City CCC-123,Sample Villa D Room 004,20190501,20190507,20190507,2,0,20191231
0000555555,Okamoto Kogoro,19520205,1,1235555,AAA Prefecture BBB City CCC-123,Sample Villa E Room 005,20190601,20190615,20190615,1,2,20191130
0000666666,Meiji Kakizaki,19811030,1,1236666,AAA Prefecture BBB City CCC-123,Sample Villa F Room 006,20190601,20190608,20190608,1,2,20191129
0000777777,Jojima Kanako,19721103,2,1237777,AAA Prefecture BBB City CCC-123,Sample Villa G 007 Room,20190601,20190608,20190608,2,1,20191030
0000888888,Kugimiya Ayabu,19330303,2,1238888,AAA Prefecture BBB City CCC-123,Sample Villa H 008 Room,20190501,20190507,20190507,1,1,20190401
0000999999,Keido Kankuro,19680229,1,1239999,AAA Prefecture BBB City CCC-123,Sample Villa I Room 009,20190601,20190615,20190615,2,0,20190331
1111000000,Koyanagi Sarujuro,19200222,1,1240000,AAA Prefecture BBB City CCC-123,Sample Villa J Room 010,20190501,20190507,20190507,2,1,20190228
1111111111,Juichiro Sado,19430130,1,1241111,AAA Prefecture BBB City CCC-123,Sample Villa K 011 Room,20190601,20190608,20190608,1,0,20190227
1111222222,Shishido Jujiro,19530301,1,1242222,AAA Prefecture BBB City CCC-123,Sample Villa L 012,20190501,20190507,20190507,2,1,20190131
Confirmation output
We are processing the first person....
Date of birth is 1986/6/It is 2.
Taichiro Ando with the number 0000111111 is 1986/6/Born 2
file output >> 0000111111,Taichiro Ando,19860602,1,1231111,AAA Prefecture BBB City CCC-123,Sample Villa A Room 001,20190501,20190507,20190507,1,0,20200229
We are processing the second person....
Date of birth is 1992/5/It is 5.
Ito Sumijiro with the number 0000222222 is 1992/5/Born 5
file output >> 0000222222,Ito Sumijiro,19920505,1,1232222,AAA Prefecture BBB City CCC-123,Sample Villa B Room 002,20190501,20190514,20190514,2,1,20200228
We are processing the third person....
Date of birth is 1999/9/It is 9.
Ukai Tasae with number 0000333333 is 1999/9/Born in 9.
file output >> 0000333333,Cormorant fishing,19990909,2,1233333,AAA Prefecture BBB City CCC-123,Sample Villa C Room 003,20190501,20190514,20190514,1,2,20200131
We are processing the 4th person....
Date of birth is 1987/8/It is 31.
Koshiro Esaki with the number 0000444444 is 1987/8/Born 31
file output >> 0000444444,Koshiro Esaki,19870831,1,1234444,AAA Prefecture BBB City CCC-123,Sample Villa D Room 004,20190501,20190507,20190507,2,0,20191231
We are processing the 5th person....
Date of birth is 1952/2/It is 5.
Kogoro Okamoto with the number 0000555555 is 1952/2/Born 5
file output >> 0000555555,Okamoto Kogoro,19520205,1,1235555,AAA Prefecture BBB City CCC-123,Sample Villa E Room 005,20190601,20190615,20190615,1,2,20191130
We are processing the 6th person....
Date of birth is 1981/10/It's 30.
Meiji Kakizaki with the number 0000666666 is 1981/10/Born 30
file output >> 0000666666,Meiji Kakizaki,19811030,1,1236666,AAA Prefecture BBB City CCC-123,Sample Villa F Room 006,20190601,20190608,20190608,1,2,20191129
We are processing the 7th person....
Date of birth is 1972/11/It is 3.
Kanako Jojima with the number 0000777777 is 1972/11/Born 3
file output >> 0000777777,Jojima Kanako,19721103,2,1237777,AAA Prefecture BBB City CCC-123,Sample Villa G 007 Room,20190601,20190608,20190608,2,1,20191030
We are processing the 8th person....
Date of birth is 1933/3/It is 3.
Kugimiya Ahachibu with the number 0000888888 is 1933/3/Born 3
file output >> 0000888888,Kugimiya Ayabu,19330303,2,1238888,AAA Prefecture BBB City CCC-123,Sample Villa H 008 Room,20190501,20190507,20190507,1,1,20190401
We are processing the 9th person....
Date of birth is 1968/2/It's 29.
Keido Kankuro with the number 0000999999 is 1968/2/Born 29.
file output >> 0000999999,Keido Kankuro,19680229,1,1239999,AAA Prefecture BBB City CCC-123,Sample Villa I Room 009,20190601,20190615,20190615,2,0,20190331
We are processing the 10th person....
Date of birth is 1920/2/It's 22.
Koyanagi Sarjuro with number 1111000000 is 1920/2/22 Born.
file output >> 1111000000,Koyanagi Sarujuro,19200222,1,1240000,AAA Prefecture BBB City CCC-123,Sample Villa J Room 010,20190501,20190507,20190507,2,1,20190228
We are processing the 11th person....
Date of birth is 1943/1/It's 30.
Juichiro Sado with the number 1111111111 is 1943/1/Born 30
file output >> 1111111111,Juichiro Sado,19430130,1,1241111,AAA Prefecture BBB City CCC-123,Sample Villa K 011 Room,20190601,20190608,20190608,1,0,20190227
We are processing the 12th person....
Date of birth is 1953/3/It is 1.
Jujiro Shishido with the number 1111222222 is 1953/3/Born 1
file output >> 1111222222,Shishido Jujiro,19530301,1,1242222,AAA Prefecture BBB City CCC-123,Sample Villa L 012,20190501,20190507,20190507,2,1,20190131
A total of 12 cases have been processed.
Can it be used as a template for simple processing or aggregation of item values held by CSV data?
Recommended Posts