"If it is the csv file in the first photo, I would like to extract only ""email address,"" ""last name,"" ""first name,"" and ""address"" and combine cells in the ""last name"" and ""first name"" columns as shown in the second photo."
There are two lines in the picture, but it will increase more, so please tell me how to automatically extract specific columns in the csv file and combine specific cells to display them.
How to Use Polars Library
import polars as pl
import io
csv_file = io.StringIO('''
Email, Last Name, First Name, Birthplace, Address
[email protected] , tanaka, taro, Hokkaido, Tokyo 1-1-1
[email protected] ,sato,hanako,Osaka Prefecture,Kanagawa Prefecture 1-1-2
[email protected] ,takahasi,jiro,Osaka Prefecture,Chiba Prefecture 1-1-3
[email protected],cat,tama,,埼玉県1-1-4
[email protected],dog,pochi,,茨城県1-1-5
''')
df = pl.read_csv(csv_file)
df.select([
'Email',
(pl.col('Surname') +' ' +pl.col('First Name')).alias('Surname'),
'Address',
]).write_csv()
# e-mail, first and last names, address
# # [email protected],tanaka taro,東京都1-1-1
# [email protected] ,satohanako,Kanagawa Prefecture 1-1-2
# # [email protected],takahasi jiro,千葉県1-1-3
# # [email protected],cat tama,埼玉県1-1-4
# # [email protected],dog pochi,茨城県1-1-5
標準モジュールのcsvから、DictReaderとDictWriterを使う方法です。
Sample Code
import csv
# "If you want to create a sample file of the original file, run ""~"" as a preparation."
"""
def create_sample_csv():
with open("source.csv", "w") as f:
f.write('email, last name, first name, place of origin, address
[email protected] , tanaka, taro, Hokkaido, Tokyo 1-1-1
[email protected] ,sato,hanako,Osaka Prefecture,Kanagawa Prefecture 1-1-2
[email protected] ,takahasi,jiro,Osaka Prefecture,Chiba Prefecture 1-1-3
[email protected],cat,tama,,埼玉県1-1-4
[email protected],dog,pochi,,茨城県1-1-5
''')
create_sample_csv()
"""
# Load source.csv to shape and convert required columns into dictionary lists
dicts = [] # Dictionary List
with open("source.csv", "r") as f:
reader = csv.DictReader(f)
for row in reader:
# dictionary row data
dict = {"Mail": row["Mail"],
"Surname and First Name": row["Surname"] + " + row["First Name"],
"Address": row["Address"]}
dicts.append(dict)
# Output result.csv based on dictionary list
with open("result.csv", "w") as f:
writer = csv.DictWriter(f, dict.keys(), lineterminator='\n')
writer.writeheader() # header output
writer.writerows(dicts) # row data output
© 2024 OneMinuteCode. All rights reserved.