2.2筛选特定的行:
- 行中的值满足某个条件
- 行中的值属于某个集合
- 行中的值匹配于某个模式(即:正则表达式)
2.2.1:行中的值满足于某个条件:
- 基础python版:
1 #!/usr/bin/env python3 2 import csv 3 import sys 4 5 input_file = sys.argv[1] 6 output_file = sys.argv[2] 7 8 with open(input_file, 'r', newline = '') as csv_in_file: 9 with open(output_file, 'w', newline = '') as csv_out_file: 10 filereader = csv.reader(csv_in_file) 11 filewriter = csv.writer(csv_out_file) 12 header = next(filereader) # 使用CSV模块的next函数读出输入文件的第一行 13 filewriter.writerow(header) # 将标题写入输出文件 14 for row_list in filereader: 15 supplier = str(row_list[0]).strip() # 取出每行数据中的供应商名字,赋值给supplier变量 16 cost = str(row_list[3]).strip('$').replace(',', '') # 使用列表索引 17 if supplier =='Supplier Z' or float(cost) > 600.0: 18 filewriter.writerow(row_list)
- pandas版:
#!/usr/bin/env python3 import pandas as pd import sys input_file = sys.argv[1] output_file = sys.argv[2] data_frame = pd.read_csv(input_file) data_frame['Cost'] = data_frame['Cost'] = data_frame['Cost'].str.strip('$').astype(float) data_frame_value_meets_condition = data_frame.loc[(data_frame['Supplier Name'].str.contains('Z')) | (data_frame['Cost'] > 600.0), :] data_frame_value_meets_condition.to_csv(output_file, index = False)
2.2.2:行中的值属于某个集合:
- 基础python:
1 #!/usr/bin/env python3 # 需求目的:保留那些购买日属于['1/20/14','1/30/2014'] 2 import csv 3 import sys 4 5 input_file = sys.argv[1] 6 output_file = sys.argv[2] 7 8 important_dates = ['1/20/2014', '1/30/2014'] # 创建了一个列表的名为important_dates的集合,important_dates是一个列表变量,它就是要属于的集合 9 10 with open(input_file, 'r', newline = ' ') as csv_in_file: 11 with open(output_file, 'w', newline = ' ') as csv_out_file: 12 filereader = csv.reader(csv_in_file) # 使用CSV模块,的reader函数,创建一个文件读取对象,名为filereader,它可以用于读取文章中的行 13 filewriter = csv.writer(csv_out_file) # 使用CSV模块的writer函数,创建了一个文件输出对象,名为filewriter,他可以用于将这个对象的数据写入输出文件 14 header = next(filereader) # 使用CSV模块的next函数,读出输入文件的第一行 15 filewriter.writerow(header) # 将header——标题行,写入输出文件 16 for row_list in filereader: # 遍历读取的文章的每一行 17 a_date = row_list[4] # 得到每一行的第5列信息,即为每一行的购买的信息,并将其赋值给变量a_date;这里使用的是索引值4 18 if a_date in important_dates: # 判断变量a_date是否属于important_dates这个集合 19 filewriter.writerow(row_list) # 如果是,则将该行数据写入输出文件
pandas:
1 #!/usr/bin/env python3 2 3 import pandas as pd 4 import sys 5 6 input_file = sys.argv[1] 7 output_file = sys.argv[2] 8 9 data_frame = pd.read_csv(input_file) # 读取输入文件,将其读取成dataframe的形式 10 data_frame_value_in_set = data_frame.loc[data_frame['Purchase Date'].isin(important_dates), :] # pandas的简洁命令:isin() 11 12 data_frame_value_in_set.to_csv(output_file,index = False) # 将data_frame_value_in_set的变量值,转换成CSV的形式,写入到输出文件中
行中的值,匹配于某个正则表达式:
- 基础python
1 #!/usr/bin/env python3 2 import csv 3 import re # 导入正则表达式模块(re) 4 import sys 5 input_file = sys.argv[1] 6 output_file = sys.argv[2] 7 pattern = re.compile(r'(?P<my_pattern_group>^001-.*)', re.I) # 使用re模块的compile函数,创建一个名为pattern的正则表达式的变量 8 with open(input_file, 'r', newline = ' ') as csv_in_file: 9 with open(output_file, 'w', newline = ' ') as csv_in_file: 10 filereader = csv.reader(csv_in_file) 11 filewriter = csv.writer(csv_out_file) 12 header = next(filereader) 13 fliewriter.writerow(header) 14 for row_list in filereader: 15 invoice_number = row_list[1] # 16 if pattern.search(invoice_number): # 使用re模块的search函数在invoice_number的值中寻找模式 17 filewriter.writerow(row_list) # 如果模式出现在invoice_number中,就将这行内容写入输出文件中
-
pandas
1 #!/usr/bin/env python3 2 3 import pandas as pd 4 import sys 5 6 input_file = sys.argv[1] 7 output_file = sys.argv[2] 8 9 data_frame = pd.read_csv(input_file) 10 data_frame_value_matches_pattern = data_frame.loc[data_frame['Invoice Number'].str.startswith("001-"), :] 11 data_frame_value_matches_pattern.to_csv(output_file, index = False)