簡體   English   中英

Python CSV特定行提取

[英]Python CSV specific line to extract

我有兩個CSV文件,我希望python打開file1.csv並從該文件讀取第7行,並在WHOLE file2.csv上查找相同的二進制代碼。

這是我到目前為止,但它不起作用:

import csv

a = open('file1.csv','r').readline[7]


with open('file2.csv') as infile:
    for row in csv.reader(infile):
        if row[1:] == a: # This part is fine because i want to skip the first row 
            print row[0], ','.join(row[1:])

看起來你需要閱讀python csv庫的工作方式:)你可能還想了解列表切片的工作原理。 我會根據我對你的問題的理解來幫助你。

我有同樣的問題, @ oliver-w有,但我只是假設你的'csv'文件只有一列。

import csv

with open('file1.csv', 'r') as file1:
    # this is the value you will be searching for in file2.csv
    # you might need to change this to [6] if there is no header row in file1.csv
    val = list(csv.reader(file1))[7]

with open('file2.csv', 'r') as file2:
    reader = csv.reader(file2)
    reader.next() # this skips the first row of the file
    # this iteration will start from the second row of file2.csv
    for row in reader:
        if row[0] == val:
            # your question doesn't clarify what your actual purpose is
            # so i don't know what should be here

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM