#!/usr/bin/python
# -*- coding: utf-8 -*-
# encoding=utf-8
# Filename:states_code.py
import os,csv,datetime
#得到當前時間
now = datetime.datetime.now()
#轉換爲指定的格式:
otherStyleTime = now.strftime("%Y%m%d%H%M%S")
#設置輸入和輸出文件
path=unicode("D:/數據集/zsc/207數據集/","utf8")
filename = "Iris.csv"
BASE_DIR = os.path.dirname(__file__)
file_in = path+filename
file_out = BASE_DIR + "/" + "output"+otherStyleTime + ".csv"
print file_in
print file_out
# ###獲取csv文件的前N行,並寫入新的文件
with open(file_in, 'rb') as csvfile:
reader = csv.reader(csvfile)
rows = [row for row in reader]
# print rows[:2]
writer = csv.writer(open(file_out, 'wb'))
for row in rows:
writer.writerow(row)
# ##獲取前N列,並將數據寫入新的文件
with open(file_in, 'rb') as csvfile:
reader = csv.reader(csvfile)
rows = [row[:2] for row in reader]
# print rows
writer = csv.writer(open(file_out, 'wb'))
for row in rows:
writer.writerow(row)
##按行合併數據
with open(file_in, 'rb') as csvfile:
reader = csv.reader(csvfile)
rows = [row for row in reader]
# print rows[0]
writer = csv.writer(open(file_out, 'wb'))
writer.writerow(rows[0])
for i in range(2):
for row in rows[1:]:
writer.writerow(row)
##按列合併數據
with open(file_in) as csvfile:
rows = csv.reader(csvfile)
with open(file_out, 'wb+') as f:
writer = csv.writer(f)
for row in rows:
row = row + row[1:]
print row writer.writerow(row)