Python:忽略csv文件中的特定行
我正在尝试创建一个简单的折线图,用来比较两个文件中的列。我写了一些代码,但想知道如何忽略这两个.csv文件中的某些行。我的代码如下:
import numpy as np
import csv
from matplotlib import pyplot as plt
def read_cell(x, y):
with open('Illumina_Heart_Gencode_Paired_End_Novel_Junctions.csv', 'r') as f:
reader = csv.reader(f)
y_count = 0
for n in reader:
if y_count == y:
cell = n[x]
return cell
y_count += 1
print(read_cell(6, 932)
def read_cell(x, y):
with open('Illumina_Heart_RefSeq_Paired_End_Novel_Junctions.csv', 'r') as f:
reader = csv.reader(f)
y_count = 0
for n in reader:
if y_count == y:
cell = n[x]
return cell
y_count += 1
print(read_cell(6, 932))
d1 = []
for i in set1:
try:
d1.append(float(i[5]))
except ValueError:
continue
d2 = []
for i in set2:
try:
d2.append(float(i[5]))
except ValueError:
continue
min_len = len(d1)
if len(d2) < min_len:
min_len = len(d2)
d1 = d1[0:min_len]
d2 = d2[0:min_len]
plt.plot(d1, d2, 'r*')
plt.plot(d1, d2, 'b-')
plt.xlabel('Data Set 1: PE_NJ')
plt.ylabel('Data Set 2: PE_SJ')
plt.show()
第一个csv文件有932行,第二个文件有99,154行。我只想从两个文件中取前932行,然后比较这两个文件中的第7列。
我该怎么做呢?
第一个文件的内容是这样的:
chr1 1718493 1718764 2 2 0 12 0 24
chr1 8928117 8930883 2 2 0 56 0 24
chr1 8930943 8931949 2 2 0 48 0 25
chr1 9616316 9627341 1 1 0 12 0 24
chr1 10166642 10167279 1 1 0 31 1 24
第二个文件的内容是这样的:
chr1 880181 880421 2 2 0 15 0 21
chr1 1718493 1718764 2 2 0 12 0 24
chr1 8568735 8585817 2 2 0 12 0 21
chr1 8617583 8684368 2 2 0 14 0 23
chr1 8928117 8930883 2 2 0 56 0 24
3 个回答
0
编辑:在函数定义中添加了分隔符选项和精度
如果你只想保留一列数据,并且在读取到一定行数后停止,可以在循环中把值添加到一个列表里,然后在列表用完时停止。但如果你的文件使用的分隔符不是逗号(,
),你就需要指定它。而且不要重复定义函数:一个def
就够了。所以你的读取函数可以像这样:
def read_column(file_name, x, y):
cells = []
with open(file_name, 'r') as f:
reader = csv.reader(f, delimiter="\t")
y_count = 0
for n in reader:
y_count += 1
if y_count > y:
break
cells.append(n[x])
return cells
这样,函数会返回一个包含x
列的列表,列表的长度是y
行
0
你的文件不是用逗号分隔的,这其实让事情变得简单一些。我们会先查看第一个文件,把每一行用空格(包括制表符和空格)分开,然后取出每一行的第七个项目。接着,我们对第二个文件做同样的事情,不过如果超过第932行,我们就会停止这个过程。
我会这样做:
file1_values = []
file2_values = []
with open('file1') as f1:
for line in f1:
seventh_column = line.split()[6]
file1_values.append(seventh_column)
with open('file2') as f2:
for i, line in enumerate(f2):
if i > 932:
break
seventh_column = line.split()[6]
file2_values.append(seventh_column)
这样一来,你就会得到两个列表,里面是你关心的值,长度应该是差不多的。接下来你可以根据这些值进行比较或者绘图,随便你怎么做。
1
一种可能的方法是先读取第一个(较短的)文件中的所有行,找出它的行数(N),然后从第二个文件中读取N行,再从两个文件中提取你感兴趣的第k
列。
可以像这样做(根据你的情况调整分隔符):
def read_tsv_file(fname): # reads the full contents of tab-separated file (like you have)
return list(csv.reader(open(fname, 'rb'), delimiter='\t'))
def take_nth_column(first_array, second_array, n): # returns a tuple containing nth columns from both arrays, with length corresponding to the length of the smaller array
len1 = len(first_array)
len2 = len(second_array)
min_len = len1 if len1<=len2 else len2
col1 = [row[n] for row in first_array[:min_len]]
col2 = [row[n] for row in second_array[:min_len]]
return (col1, col2)
first_array = read_tsv_file('your-first-file')
second_array = read_tsv_file('your-second-file')
(col1, col2) = take_nth_column(first_array, second_array, 7)