【发布时间】:2020-05-04 06:20:54
【问题描述】:
我有一个 CSV 文件,我必须从中创建另一个特定格式的 CSV。我正在共享我的输入 CSV 和输出 CSV 文件(我通过我的代码获得)和所需的输出 CSV 格式。请帮助我实现这一目标。 输入样本文件:
MSISDN,REQUEST_ID,STATE1,STATE2,STAETE3,NOTIFICATION
22969000034,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,ENTERED,NA,NA,NA
22969000034,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,NA,IN_PROGRESS,NA,NA
22969000034,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,NA,NA,COMPLETED,Successfully send SMS to the subscriber
22969000035,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,ENTERED,NA,NA,NA
22969000035,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,NA,IN_PROGRESS,NA,NA
22969000035,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,NA,NA,COMPLETED,Successfully send SMS to the subscriber
22969000036,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,ENTERED,NA,NA,NA
22969000036,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,NA,IN_PROGRESS,NA,NA
22969000037,OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27,ENTERED,NA,NA,NA
输出样本数据:
('22969000034', 'OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27', 'ENTERED', 'NA', 'NA', 'NA')
('22969000034', 'OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27', 'ENTERED', 'IN_PROGRESS', 'NA', 'NA')
('22969000034', 'OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27', 'ENTERED', 'IN_PROGRESS', 'COMPLETED', 'Successfully send SMS to the subscriber')
('22969000035', 'OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27', 'ENTERED', 'NA', 'NA', 'NA')
('22969000035', 'OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27', 'ENTERED', 'IN_PROGRESS', 'NA', 'NA')
('22969000035', 'OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27', 'ENTERED', 'IN_PROGRESS', 'COMPLETED', 'Successfully send SMS to the subscriber')
('22969000036', 'OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27', 'ENTERED', 'NA', 'NA', 'NA')
('22969000036', 'OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27', 'ENTERED', 'IN_PROGRESS', 'NA', 'NA')
('22969000037', 'OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27', 'ENTERED', 'NA', 'NA', 'NA')
所需样本数据:
('22969000034', 'OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27', 'ENTERED', 'IN_PROGRESS', 'COMPLETED', 'Successfully send SMS to the subscriber')
('22969000035', 'OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27', 'ENTERED', 'IN_PROGRESS', 'COMPLETED', 'Successfully send SMS to the subscriber')
('22969000036', 'OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27', 'ENTERED', 'IN_PROGRESS', 'NA', 'NA')
('22969000037', 'OFFLINE_Notification_10.10.46.95_e6444dbc-b7ef-41d6-9111-eaa384717b27', 'ENTERED', 'NA', 'NA', 'NA')
我正在使用以下代码:
with open('msLog.csv', 'r') as readfile:
output = csv.DictReader(readfile)
f_list = []
msd = ''
id = ''
st1 = ''
st2 = ''
st3 = ''
txt = ''
for row in output:
msd1 = row.get('MSISDN')
if msd1 and row.get('STAETE3') != 'NA' and row.get('NOTIFICATION') != 'NA':
msd = msd1
id = row.get('REQUEST_ID')
st3 = row.get('STAETE3')
txt = row.get('NOTIFICATION')
elif msd1 and row.get('STAETE3') == 'NA' and row.get('STATE1') =='NA':
st2 = row.get('STATE2')
id = row.get('REQUEST_ID')
msd = msd1
#st1 = 'NA'
st3 = 'NA'
txt = 'NA'
elif msd1 and row.get('STAETE3') == 'NA' and row.get('STATE2') =='NA':
st1 = row.get('STATE1')
id = row.get('REQUEST_ID')
msd = msd1
st2 = 'NA'
st3 = 'NA'
txt = 'NA'
print(msd,id,st1,st2,st3,txt)
【问题讨论】:
-
请说出您的问题,您刚刚发布了所需的输出,但没有说明如何获得该输出。
标签: python csv dictionary