在 Python 中将参数变量设置为 CSV 值

Setting Parameter variable to CSV value in Python

我在从 CSV 中读取地址并将参数设置为结果时遇到错误。当代码

    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for location in csv_reader:
            print(' ADDRESS: ' + location[0])
# defining a params dict for the parameters to be sent to the API
# PARAMS = {'ff':location[0]}

是运行结果是

 ADDRESS: 527 NE MONROE ST, PORTLAND, OR
 ADDRESS: 1129 SW 20TH AVE, PORTLAND, OR
 ADDRESS: 6511 N BURRAGE AVE, PORTLAND, OR

当行

# PARAMS = {'ff':location[0]}

未注释,但是,我收到此错误

... PARAMS = {'ff':location[0]}
  File "<stdin>", line 7
    PARAMS = {'ff':location[0]}
    ^
SyntaxError: invalid syntax

你知道如何解决这个问题吗?我是 Python 的新手。我搜索了该网站上的帖子,但没有找到解决此问题的任何内容。

Python 对缩进非常敏感。考虑使用带有 python 扩展名的 IDE,例如 vscode,以防止出现此类问题

考虑到您的评论,我假设您的用例如下:您想要检索第一个元素,即位置的地址,并且有很多位置(每行一个)。

在这种情况下,您可以执行以下操作,这很容易理解:

    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    fst_elements_of_locations = []
    for location in csv_reader:
            print(' ADDRESS: ' + location[0])
            # add address of location into list 
            fst_elements_of_locations.append(location[0])
     # params['ff'] contains a list of all the address of locations
     PARAMS = {'ff':fst_elements_of_locations}

或更高效

    csv_reader = csv.reader(csv_file, delimiter=',')
    PARAMS = {'ff':[location[0] for location in csv_reader]}