[英]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,如有必要可以安装任何软件包。
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.