CSV (Comma Separated Values) format is the most common format in computer world (export and import). In python 'csv module' implements classes to read and write tabular data in CSV format without knowing the precise details of the CSV format used by Excel.
Here it is reading csv file and filtering data in it. Create new CSV file and moved filtered data
1 import csv
2
3 data = []
4
5 #reading csv file
6 with open('D:/Research/python/data/class.csv', 'rb') as f:
7 reader = csv.reader(f)
8 #checking file is open fine
9 print f.closed
10 count =0
11 for row in reader:
12 print row
13 #catching first element
14 if count ==0 :
15 data += [row]
16 #collecting over 99 marks only
17 else:
18 if int(row[1]) > 99:
19 data += [row]
20 count += 1
21 #f.close();
22
23 #writting to csv file
24 with open('D:/Research/python/data/some.csv', 'wb') as f1:
25 writer = csv.writer(f1)
26 for row in data:
27 print row
28 writer.writerows(data)
out put
%run D:/Research/python/CSV1.py
False
['name', 'marks']
['dilan', '100']
['jone', '98']
['james', '100']
['jack', '92']
['dilan', '100']
['james', '100']
No comments:
Post a Comment