with open('/home/vivek/Desktop/input.csv', 'rb') as infile:
with open('/home/vivek/Desktop/output.csv', 'wb') as outfile:
reader = csv.reader(infile, delimiter='\t')
writer = csv.writer(outfile, delimiter='\t')
reader_row = next(reader)
writer.writerow([reader_row[0], reader_row[1]])
for row in reader:
if row[1]=="H":
writer.writerow(row)
with open('input.txt', 'r') as input_file:
input_lines = input_file.readlines()
# Take the header line, and all the subsequent lines whose last character is 'H'
output_lines = input_lines[:1] + [line for line in input_lines if line[-2] == 'H']
output_string = ''.join(output_lines)
with open('output.txt', 'w') as output_file:
output_file.write(output_string)
with open('input.txt', 'r') as input_file:
output_lines = (line for i, line in enumerate(input_lines)
if line[-2] == 'H' or i == 0)
with open('output.txt', 'w') as output_file:
for line in output_lines:
output_file.write(line)
刚刚更新了针对python2.7.3的inspectorG4dget解决方案:
只考虑输入csv文件中的两列,它们是由
ID
和type
分隔的\t
代码:
输出:
检查下面的2.6.6我没有测试下面的python2.6.6代码,因为我的机器上有python2.7.3。你知道吗
假设您的文件只是一个文本文件,用空格/制表符分隔列,并且包含“type”的列正好位于行的末尾:
上面的代码假设“type”列在单字符类型代码之后立即结束。如果数据后面可能有空格,或者可能有类似“AH”等的多字符类型代码,则将注释下面的行替换为以下行:
编辑:如果您的文件非常大,并且您不想将其全部加载到内存中并进行操作,则可以使用生成器表达式,该表达式将被延迟计算:
相关问题 更多 >
编程相关推荐