import csv
f = open("example.csv")
f_read = csv.reader(f)
f_data = list(f_read)
#print (f_data)
#print (f_data[2][2])
for row in f_read:#使用for循环,不一次性写入内存
print ('Row#' + str(f_read.line_num) + ' ' + str(row))
outputFile = open('output.csv', 'w', newline = '')
#注意newline使用
outputWrite =csv.writer(outputFile)
outputWrite.writerow(['a', 'b', 'c', 'd'])
print(outputFile)
outputFile.close()
openfile = open("output.csv")
openfile_red = csv.reader(openfile)
open_data = list(openfile_red)
print (open_data)
#不用逗号分隔 a b c ,分隔符
csvWrite = csv.write(csvFile, delimter='\t',lineterminator='\n\n')
]]#! python3
# removeCsvHeader.py - Removes the header from all CSV files in the current
# working directory.
import csv, os
os.makedirs('headerRemoved', exist_ok=True)
# Loop through every file in the current working directory.
for csvFilename in os.listdir('.'):
if not csvFilename.endswith('.csv'):
continue # skip non-csv files
print('Removing header from ' + csvFilename + '...')
# Read the CSV file in (skipping first row).
csvRows = []
csvFileObj = open(csvFilename)
readerObj = csv.reader(csvFileObj)
for row in readerObj:
if readerObj.line_num == 1:
continue # skip first row
csvRows.append(row)
csvFileObj.close()
# Write out the CSV file.
csvFileObj = open(os.path.join('headerRemoved', csvFilename), 'w', newline='')
csvWriter = csv.writer(csvFileObj)
for row in csvRows:
csvWriter.writerow(row)
csvFileObj.close()