Python:忽略csv文件中的特定行
我试图创建一个简单的折线图来比较两个文件中的列。我已经写了一些代码,想知道如何忽略我拥有的两个.csv文件中的行。代码如下:Python:忽略csv文件中的特定行,python,csv,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:
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()
第二个文件如下所示:
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
因此,您的文件不是逗号分隔的,这实际上使这更容易。我们遍历第一个文件,在用空格(用于分隔数据中项目的制表符/空格)分隔行之后,获取每行中的第7项。然后我们对下一个文件做同样的事情,但是如果我们超过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)
然后,您将感兴趣的值放入两个长度相等的列表中,并可以从此列表开始执行您希望执行的任何比较或绘图操作。编辑:在函数定义中添加分隔符选项和精度 如果您只想保留一列并在一行计数后停止读取,只需将值附加到循环中的列表中,并在循环用尽时中断。但如果您的文件使用逗号(
,
)以外的任何东西作为分隔符,则必须指定它。不要重复函数定义:一个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
第一行一种可能的方法是从第一个(较短)文件中读取所有行,找出其长度(N),从第二个文件中读取N行,从两个文件中获取感兴趣的k
第列
类似于(调整案例的分隔符):
def read_tsv_文件(fname):#读取选项卡分隔文件的全部内容(就像您所做的那样)
返回列表(csv.reader(打开(fname,'rb'),分隔符='\t'))
def take_nth_column(第一个数组,第二个数组,n):#返回一个包含两个数组中第n列的元组,其长度与较小数组的长度相对应
len1=len(第一个数组)
len2=len(第二个_数组)
如果len1csv以逗号分隔,则min_len=len1。逗号分隔值..@ChrisArena我对这类东西不熟悉。CSV文件与.txt文件有何不同?我通过执行head-5“filename”得到这个输出。CSV文件中的条目用逗号分隔。您的文件包含由选项卡分隔的条目。也有以选项卡分隔的CSV,但它们很少受到支持
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
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)