簡體   English   中英

在Python中,如何找到CSV文件中信息的位置?

[英]In Python, how can I find the location of information in a CSV file?

我有三個非常長的CSV文件,並且在處理代碼時需要一些建議/幫助。 基本上,我希望程序足夠廣泛/基本,可以添加任何限制,並且可以正常運行。

例如,如果我想設置代碼以查找列1 == x和列2 == y的位置,那么我想讓代碼也能在列1!= r和列2的情況下工作

import csv
file = input('csv files: ').split(',')
filters = input('Enter the filters: ').split(',')
f = open(csv_file,'r')
p=csv.reader(f)
header_eliminator = next(p,[])

我遇到了“文件”部分的問題,因為如果我選擇僅使用一個文件而不是現在要使用的三個文件,它將無法正常工作。 過濾器也是如此。 過濾器可能像4 == 10,5> = 4

這意味着文件的第4列等於10,文件的第5列等於或大於4。但是,我也可能希望過濾器看起來像這樣:1 == 4.333,5 == “ 6/1/2014 0:00:00”,6 <= 60.0,7!= 6

因此,我希望能夠將其用於其他用途! 我對此有很多麻煩,您對如何開始有任何建議嗎? 謝謝!

熊貓非常適合處理csv文件。 我建議安裝它。 pip install pandas

然后,如果您想讀取打開的3個csv文件並在列上進行檢查。 您只需要熟悉熊貓索引 您現在需要知道的唯一方法是.iloc因為似乎您正在使用列的整數位置建立索引。

import pandas as pd

files = input('Enter the csv files: ').split(',')
data = []
#keeping a list of the files allows us to input a different number of files
#we use pandas to read in each file into a pandas dataframe which is then     stored in an element of the list. The length of the list is the number of files.
for names in files:
    data.append(pd.read_csv(names)

#You can then perform checks like this to see if the column 2 of all files are equal to 3
print all(i.iloc[:,2] == 3 for i in data)

您可以編寫一個生成器,該生成器將使用一堆文件名並csv.reader輸出行,並將其輸入到csv.reader 棘手的部分是過濾器。 如果讓過濾器成為一行python代碼,則可以將eval用於該部分。 舉個例子

import csv

#filenames = input('csv files: ').split(',')
#filters = input('Enter the filters: ').split(',')

# todo: for debug
# in this implementation, filters is a single python expression that can
# reference the 'col' variable which is a list of the current columns
filenames = 'a.csv,b.csv,c.csv'
filters = '"a" in col[0] and "2" in col[2]'

# todo: debug generate test files
for name in 'abc':
    with open('{}.csv'.format(name), 'w') as fp:
        fp.write('the header row\n')
        for row in range(3):
            fp.write(','.join('{}{}{}'.format(name, row, col) for col in range(3)) + '\n')

def header_squash(filenames):
    """Iterate multiple files line by line after squashing header line
    and any empty lines.
    """
    for filename in filenames:
        with open(filename) as fp:
            next(fp)
            for line in fp:
                if line.strip():
                    yield line

for col in csv.reader(header_squash(filenames.split(','))):
    # eval's namespace limits the damage untrusted code can do...
    if eval(filters, { 'col':col }):
        # passed the filter, do the work
        print(col)

暫無
暫無

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

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