Python CSV嵌套用于循环字符串替换

时间:2015-02-23 21:59:08

标签: csv python-3.x for-loop nested str-replace

我试图说:对于campaigns[0]中的每一个(即:"New York City", "DC", "New York", "Boston", "Detroit", "St. Louis", "Cleveland"),然后为CSV中的每个row[2],请替换为[city_name], [state_abbrev], [state_full] campaigns[1], campaigns[2], campaigns[3]

import csv

campaigns = [
            ["New York City", "New York City", "New York", "NY"],
            ["DC", "Washington DC", "Washington DC", "DC"],
            ["New York", "New York", "New York", "NY"],
            ["Boston", "Boston", "Massachusetts", "MA"],
            ["Detroit", "Detroit", "Michigan", "MI"],
            ["St. Louis", "St Louis", "Missouri", "MO"],
            ["Cleveland", "Cleveland", "Ohio", "OH"]
            ]

with open('ExpandCampaign.csv', 'r') as f:
  reader = csv.reader(f)
  print(next(reader, None))  # Print the header
  for campaign in campaigns:
    for row in reader:
      row[0] = campaign[0]
      row[2] = row[2].replace('[city_name]', campaign[1].lower())
      row[2] = row[2].replace('[state_full]', campaign[2].lower())
      row[2] = row[2].replace('[state_abbrev]', campaign[3].lower())
      print(row)

问题:上面的代码只输出纽约市,而不是广告系列[0]中的其他广告系列名称。

['New York City', 'Design', 'basement design', 'Exact']
['New York City', 'Floor Plans', 'basement design plans', 'Exact']
['New York City', 'Design', 'basement designs', 'Exact']
['New York City', 'Finishing', 'basement finished remodeling', 'Exact']
['New York City', 'Finishing', 'basement finishers ny', 'Exact']
['New York City', 'Finishing', 'basement finishing', 'Exact']
['New York City', 'Finishing', 'basement finishing new york city', 'Exact']

我不明白为什么它不会遍历每个campaign[0]名称,而且它只能看到纽约市。例如,如果我打印:

with open('ExpandCampaign.csv', 'r') as f:
  reader = csv.reader(f)
  print(next(reader, None))  # Print the header
  for campaign in campaigns:
    print(campaign[0])

我明白了:

['Campaign', 'Ad Group', 'Keyword', 'Type']
New York City
DC
New York
Boston
Detroit
St. Louis
Cleveland

我试图改变for语句,但我得到的结果是:

['New York City', 'Finishing', 'basement finishing in new york city ny', 'Phrase']
['DC', 'Finishing', 'basement finishing in new york city ny', 'Phrase']
['New York', 'Finishing', 'basement finishing in new york city ny', 'Phrase']
['Boston', 'Finishing', 'basement finishing in new york city ny', 'Phrase']
['Detroit', 'Finishing', 'basement finishing in new york city ny', 'Phrase']
['St. Louis', 'Finishing', 'basement finishing in new york city ny', 'Phrase']
['Cleveland', 'Finishing', 'basement finishing in new york city ny', 'Phrase']

了解"纽约市"出现在非纽约市的活动中?这是我不想发生的事情,因为这些城市只与他们各自的活动相关,而不是彼此相关。我如何解决这个问题的逻辑显然不在某个地方。

2 个答案:

答案 0 :(得分:1)

您正在循环遍历“for campaign”循环的第一遍中的所有行。添加以下代码段中的打印语句,您将看到:

  for campaign in campaigns:
    print(campaign) # add this line
    for row in reader:
      row[0] = campaign[0]
      row[2] = row[2].replace('[city_name]', campaign[1].lower())
      row[2] = row[2].replace('[state_full]', campaign[2].lower())
      row[2] = row[2].replace('[state_abbrev]', campaign[3].lower())
      print(row)

也许您只想为广告系列和行添加一个循环,如下所示

for (campaign,row) in zip(campaigns, reader):
      row[0] = campaign[0]
      row[2] = row[2].replace('[city_name]', campaign[1].lower())
      row[2] = row[2].replace('[state_full]', campaign[2].lower())
      row[2] = row[2].replace('[state_abbrev]', campaign[3].lower())
      print(row)

它将在两个列表中较短的一个上终止。

答案 1 :(得分:1)

我停止使用csv.reader,而是在没有它的情况下读取文件并且它可以正常工作。我不知道为什么会这样,而csv.reader方法却没有。

campaigns = [
            ["New York City", "New York City", "New York", "NY"],
            ["DC", "Washington DC", "Washington DC", "DC"],
            ["New York", "New York", "New York", "NY"],
            ["Boston", "Boston", "Massachusetts", "MA"],
            ["Detroit", "Detroit", "Michigan", "MI"],
            ["St. Louis", "St Louis", "Missouri", "MO"],
            ["Cleveland", "Cleveland", "Ohio", "OH"]
            ]

with open('ExpandCampaign.csv', 'r') as f:
  print(next(f, None).strip().split(','))  # Print the header
  rows = [row.strip().split(',') for row in f]
  for campaign in campaigns:
    for row in rows:
      row[0] = campaign[0]
      row[2] = row[2].replace('[city_name]', campaign[1].lower())
      row[2] = row[2].replace('[state_full]', campaign[2].lower())
      row[2] = row[2].replace('[state_abbrev]', campaign[3].lower())
      print(row)