[英]How to read csv file, and add header and new columns based on data in csv and output new csv
在運行比較兩個csv文件的python腳本之后,我留下了一個新的csv文件,該文件僅列出新學生的電子郵件地址。 (稍后將使用此功能在我們的電子郵件系統中自動創建新用戶)。
但是,我需要獲取僅包含電子郵件地址的csv文件,並根據電子郵件地址中的信息,創建一個新的csv文件,其中包含firstname,lastname,email的標題,然后為每行提供相應的數據。
例:
原始csv(newemails.csv)
john.doe@mydomain.com
terry.jackson@mydomain.com
silly.sally@mydomain.com
新的CSV文件應如下所示:
firstname, lastname, email
John, Doe, john.doe@mydomain.com
Terry, Jackson, terry.jackson@mydomain.com
Silly, Sally, silly.sally@mydomain.com
這是我當前的代碼,它給了我newemails.csv文件
import csv
import os
import subprocess
def newemails():
for line in fileinput.input(r'C:\gamwork\currentstudents.csv', inplace=1):
print(line.lower(), end='')
with open(r'C:\gamwork\previoususers.csv', 'r') as t1,
open(r'C:\gamwork\currentstudents.csv', 'r') as t2:
fileone = t1.readlines()
filetwo = t2.readlines()
with open(r'C:\gamwork\newemails.csv', 'w') as outFile:
for line in filetwo:
if line not in fileone:
outFile.write(line)
我真的不確定該怎么做,任何建議都非常感謝!
你可以嘗試這個:如果你的文件包含
john.doe@mydomain.com
terry.jackson@mydomain.com
silly.sally@mydomain.com
然后你可以做以下事情:
with open('mydata.csv', 'r') as f, open('out.csv', 'w') as out_file:
out_file.write('First name, last name, email\n')
for line in f:
names, email = line.split('@')[0], line
first, last = names.split('.')
new_line = f'{first}, {last}, {email}'
out_file.write(new_line)
with open('out.csv', 'r') as out_file:
print(out_file.read())
輸出:
First name, last name, email
john, doe, john.doe@mydomain.com
terry, jackson, terry.jackson@mydomain.com
silly, sally, silly.sally@mydomain.com
這可以通過pandas
輕松實現,請在下面找到一個示例。
import pandas as pd
df = pd.read_csv('test3.csv', header=None, names=['Email'])
df['FirstName'] = [x.split('.')[0].title() for x in df['Email']]
df['LastName'] = [x.split('.')[1].split('@')[0].title() for x in df['Email']]
df = df.drop('Email', 1)
print(df)
df.to_csv('students.csv')
或另一種解決方案
import pandas as pd
def createFirstLastNames(row):
firstLast = row['Email'].split('@')[0]
firstName = firstLast.split('.')[0].title()
lastName = firstLast.split('.')[1].title()
return pd.Series({
'FirstName' : firstName,
'LastName' : lastName
})
df = pd.read_csv('test3.csv', header=None, names=['Email'])
df1 = df.merge(df.apply(lambda row: createFirstLastNames(row), axis=1), left_index=True, right_index=True)
df1 = df1.drop('Email', 1)
print(df1)
df1.to_csv('students.csv')
輸出如下
FirstName LastName
John Doe
Terry Jackson
Silly Sally
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.