Python CSV read file and select columns and write to new CSV file -


i have csv file has columns need extract. 1 of columns text string need extract first , last items. have print statement in loop need cannot figure out how either data list or dict. not sure best use.

code far:

f1 = open ("report.csv","r") # open input file reading  users_dict = {} open('out.csv', 'wb') f: # output csv file  writer = csv.writer(f) open('report.csv','r') csvfile: # input csv file     reader = csv.dictreader(csvfile, delimiter=',')     row in reader:         print row['user name'],row['address'].split(',')[0],row['last login datetime'],row['address'].split(',')[7]         users_dict.update(row)         #users_list.append(row['address'].split(','))         #users_list.append(row['last login datetime'])         #users_list.append(row[5].split(',')[7])  print users_dict  f1.close()  

input file:

user name,display name,login name,role,last login datetime,address,application,aaa,exchange,comment support,support,support,124,2015-05-29 14:32:26,"test company,bond st,london,london,1111 111,gb,test@test.com,is",,,lse, 

output on print:

support test company 2015-05-29 14:32:26 

using code, i've got line need:

import csv f1 = open ("report.csv","r") # open input file reading  users_dict = {} open('out.csv', 'wb') f: # output csv file     writer = csv.writer(f)     open('report.csv','r') csvfile: # input csv file         reader = csv.dictreader(csvfile, delimiter=',')         row in reader:             print row['user name'],row['address'].split(',')[0],row['last login datetime'],row['address'].split(',')[7]             users_dict.update(row)             #users_list.append(row['address'].split(','))             #users_list.append(row['last login datetime'])             #users_list.append(row[5].split(',')[7])      print users_dict  f1.close() 

the changes:

  • including import csv @ top.
  • indenting code after with open('out.csv' ......

does solve problem?


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -