這樣做的一種方式,沒有安裝像numpy/pandas這樣的第三方模塊,如下所示。給定一個輸入文件,名爲 「input.csv」 是這樣的:
A,B,c_net,d,e_net
0,0,1,0,1
0,0,1, 0,1
(去除之間的空行,它們只是格式化這個職位的 內容)
下面的代碼你想要做什麼。
import csv
input_filename = 'input.csv'
output_filename = 'output.csv'
# Instantiate a CSV reader, check if you have the appropriate delimiter
reader = csv.reader(open(input_filename), delimiter=',')
# Get the first row (assuming this row contains the header)
input_header = reader.next()
# Filter out the columns that you want to keep by storing the column
# index
columns_to_keep = []
for i, name in enumerate(input_header):
if 'net' in name:
columns_to_keep.append(i)
# Create a CSV writer to store the columns you want to keep
writer = csv.writer(open(output_filename, 'w'), delimiter=',')
# Construct the header of the output file
output_header = []
for column_index in columns_to_keep:
output_header.append(input_header[column_index])
# Write the header to the output file
writer.writerow(output_header)
# Iterate of the remainder of the input file, construct a row
# with columns you want to keep and write this row to the output file
for row in reader:
new_row = []
for column_index in columns_to_keep:
new_row.append(row[column_index])
writer.writerow(new_row)
請注意,沒有錯誤處理。至少應該處理兩個。第一個是檢查輸入文件是否存在(提示:檢查os和os.path模塊提供的功能)。第二個是處理空白行或列數不一致的行。
你能請附上您的文件的一個小樣本有問題,使問題陳述更清楚一點? – ZdaR
當然!我已經獲得了幫助,但是我現在包含了一段代碼樣本的小圖片,以防將來幫助任何人 – Rickyboy