output的反义词python咖啡名称提取_python–提取包含特定名称的列在没有安装像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 = ader(open(input_filename), delimiter=',')
# Get the first row (assuming this row contains the header)
input_header = ()
# 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模块提供的功能).第⼆个是处理具有不⼀致列数的空⽩⾏或⾏.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。