简体   繁体   English

如何从 .csv 文件中提取值并将它们放入数组中?

[英]How to extract values from .csv files and put them in array?

I am trying to extract values from 2 .csv files using python.我正在尝试使用 python 从 2 个 .csv 文件中提取值。 From the 1st file I am getting a keyword that would be searched in the 2nd file.从第一个文件中,我得到了一个将在第二个文件中搜索的关键字。 After the keyword is found, I am trying to collect values from a different column and put them into an array.找到关键字后,我试图从不同的列中收集值并将它们放入数组中。

This is what the 1st file looks like ( the one that I'm getting the keyword from ):这是第一个文件的样子(我从中获取关键字的文件):

Action;ItemID;Title;StartPrice;CustomLabel
Revise;13;some title;990;keyword1
Revise;12;some title;1990;keyword2
Revise;14;some title;2990;keyword3

This is what the second .csv file looks like:这是第二个 .csv 文件的样子:

Keyword;Item_Title;Item_Price;Item_Condition;Item_Link
keyword1;title1;59.0;used;link1
keyword1;title2;130.0;used;link2
keyword1;title3;165.0;used;link3
keyword2;title1a;600.0;used;link1a
keyword2;title2a;800.0;used;link2a
keyword2;title3a;899.99;used;link3a
keyword2;title4a;1895.0;new;link4a

I am trying to get the CustomLabel value and search it in the Keyword column in the 2nd file.我正在尝试获取CustomLabel值并在第二个文件的Keyword列中搜索它。 The goal is to collect the Item_Price values for each keyword value into an array.目标是将每个关键字值的Item_Price值收集到一个数组中。

So far I've tried this, but it doesn't work since it collect all the Item_Price for every keyword into one array.到目前为止,我已经尝试过这个,但它不起作用,因为它将每个关键字的所有Item_Price收集到一个数组中。

This is the code in question:这是有问题的代码:

import csv

prices=[]

with open("1.csv",'r') as file:
    file_reader=csv.DictReader(file,delimiter=';')
    for row in file_reader:
        item=row['CustomLabel']

        with open("1.csv",'r',encoding='utf-8') as file1:
            file_reader1=csv.DictReader(file1,delimiter=';')
            for row1 in file_reader1:
                if item in row1['Keyword']:
                    print(row1['Keyword']+" , "+row1['Item_Price'])
                    price=row1['Item_Price']
                    prices.append(price)
print(prices)

And as you can figure out for yourself I am getting an array with all the prices for all the keywords:正如您可以自己弄清楚的那样,我得到了一个包含所有关键字的所有价格的数组:

['59.0', '130.0', '165.0', '600.0', '800.0', '899.99', '1895.0']

My question: How do I get an array for each keyword in the file?我的问题:如何为文件中的每个关键字获取一个数组? Does my approach work at all ?我的方法有效吗? Or I need to do something differently?或者我需要做一些不同的事情? Any suggestions on how to achieve that are welcome.欢迎任何有关如何实现这一目标的建议。 I am using python 3.6.5 and could install any packages if necessary.我正在使用 python 3.6.5,如有必要可以安装任何软件包。

numpy's function np.loadtxt does just that. numpy 的函数np.loadtxt就是这样做的。 It's well documented here 这里有详细记录

Alternatively, if keeping track of your columns' titles is important to you, have a look at pandas's pd.read_csv , documented there或者,如果跟踪列的标题对您很重要,请查看 pandas 的pd.read_csv ,记录在那里

You can do it by first creating a dictionary to map each CustomLabel to a list of Item_Price s from the second file and then using that dictionary to extract them from the Keyword column in the second file您可以先创建一个字典,每个地图做CustomLabel到列表Item_Price第二个文件秒,然后使用该字典从提取出来Keyword在第二个文件列

import csv

prices = {}

with open("1.csv", 'r') as file:
    for row in csv.DictReader(file, delimiter=';'):
        custom_label = row['CustomLabel']
        if custom_label not in prices:
            prices[custom_label] = []

with open("2.csv", 'r', encoding='utf-8') as file:
    for row in csv.DictReader(file, delimiter=';'):
        keyword = row['Keyword']
        if keyword in prices:
            prices[keyword].append(row['Item_Price'])

from pprint import pprint
pprint(prices)

Output:输出:

{'keyword1': ['59.0', '130.0', '165.0'],
 'keyword2': ['600.0', '800.0', '899.99', '1895.0'],
 'keyword3': []}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何组合来自多个文件的多行并将它们放入数组 - how to combine multiple lines from multiple files and put them to an array 从列表中提取值并将它们放入 Python 中的 dataframe - Extract values from a list and put them into a dataframe in Python 如何从数据框 col 中提取特定值并将它们放在另一列中? - How to extract particular values from a dataframe col and put them in another column? 如何从文本文件中提取特定行,然后从这些提取的行中提取括号之间的值并将它们放在另一个文件中 - How to extract specific lines from a text file and then from these extracted line, extract the values between parantheses and put them in another file 如何从 msg 文件中提取数据并将它们插入(附加)到 csv 文件中? - How to extract data from msg files and insert (append) them to csv file? 如何使用循环从 CSV 中的列中提取值并使用 python 在电子邮件正文中使用它们? - How to extract values from columns in CSV using a loop and use them in email body using python? 如何从带有图像注释的 CSV 文件中提取值并将它们附加到 Python 中 RetinaNet 的新 CSV 文件中? - How can I extract values from a CSV file with image annotations and append them to a new CSV file for RetinaNet in Python? 如何从文件中提取图像并将其放入列表中,使用python - how to extract images from file and put them in a list using python 如何从 excel 列中获取所有值并将它们放入 python 中的数组中? - How do I get all values from an excel column and put them into an array in python? 如何迭代地从文本文件中提取一列以放入二维数组 - How to iteratively extract a column from text files to put into a 2D array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM