简体   繁体   English

如何从 CSV 制作字典键?

[英]How do you make a dictionary key from a CSV?

I'm having issues creating a dictionary key using a csvreader.我在使用 csvreader 创建字典键时遇到问题。 I want to create a dictionary, which contains the location column where the data was found, so that I can write it out to a new location later.我想创建一个字典,其中包含找到数据的位置列,以便以后可以将其写出到新位置。 I haven't included the write function, because I want to understand how to do the create the keys first.我没有包含 write 函数,因为我想先了解如何创建密钥。

For example, this data point 123-123-1234 was found in row[0].例如,这个数据点 123-123-1234 是在 row[0] 中找到的。

input_file_column_modification = ''
myData = []
primary_key_list = {}

if os.path.isfile(filename):
  input_file_column_modification = open(filename)
  myData = [item for item in csv.reader(input_file_column_modification)]

for row in myData:
  primary_key_pattern_match = re.search('\d{3}-\d{3}-\d{4}, row[0], re.I)
  if primary_key_pattern_match is not None:
  ** QUESTION: How do I keep track of the row/columns were the data is being found?
  primary_key_list.append(primary_key_pattern_match.group(0))

Current input being read in Note that 2 entries have no pattern to match.正在读取的当前输入请注意,2 个条目没有要匹配的模式。


Info,Address,City,ZipCode,Last Updated信息、地址、城市、邮政编码、上次更新

Lorem ipsum dolor sit amet, consectetur (123-123-1234)adipiscing elita,100 some address,cityname,"zipcode",03/24/2016 Lorem ipsum dolor sat amet, consectetur (123-123-1234)adipiscing elita,100 some address,cityname,"zipcode",03/24/2016

Lorem ipsum dolor sit amet, consectetur adipiscing elit,200 some address, cityname,zipcode,03/24/2016 Lorem ipsum dolor sat amet, consectetur adipiscing elit,200 一些地址, 城市名, 邮政编码,03/24/2016

Lorem ipsum dolor sit amet, consectetur (345-345-3456) adipiscing elit,300 some address,cityname,zipcode,03/24/2016 Lorem ipsum dolor sat amet, consectetur (345-345-3456) adipiscing elit,300 some address,cityname,zipcode,03/24/2016

Lorem ipsum dolor sit amet, consectetur adipiscing elit,400 some address, cityname,zipcode,03/24/2016 Lorem ipsum dolor sat amet, consectetur adipiscing elit,400 一些地址, cityname,zipcode,03/24/2016

Lorem ipsum dolor sit amet, consectetur (567-567-5678) adipiscing elit,500 some address,cityname,zipcode,03/24/2016 Lorem ipsum dolor sat amet, consectetur (567-567-5678) adipiscing elit,500 一些地址,城市名称,邮政编码,03/24/2016

One way of doing it would be via enumerate which gives you both the index or "iteration counter" and the value of an iterable as you loop through it:一种方法是通过enumerate ,它为您提供索引或“迭代计数器”以及循环遍历它时可迭代的值:

for row_num, row in enumerate(myData):
    primary_key_pattern_match = re.search('\d{3}-\d{3}-\d{4}, row[0]', re.I)
    if primary_key_pattern_match is not None:
        row_num_and_row_data = (row_num, row)
        # You now have a tuple whose 1st element is the row number
        # and whose 2nd element is the row (a tuple or list).

        # You can also skip making a tuple and add the row 
        # to a dictionary immediately (declare it before the loop): 
        row_dict[row_num] = row

        # or to add the results of the regex:
        row_dict[row_num] = primary_key_pattern_match.group(0)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM