使用 python,如何将新项目写入 CSV 文件中的新行

Using python, how to write new item into a new row in a CSV file

形成一个 Microsoft xlsx 文件,我正在使用 python 脚本读取包含房屋地址的列。然后,我想将其写入格式如下的 CSV 文件中:

firstname_user, secondname_user, address
firstname_user, secondname_user, address
firstname_user, secondname_user, address
firstname_user, secondname_user, address

在 python 脚本下面我必须这样做

代码:

import pandas as pd
import csv

the_xl_file = "mydata.xlsx"

original_xl = pd.ExcelFile(the_xl_file).parse('Sheet1')
address_list=[]
address_list.append(original_xl['column_name'])
 
file = open('output.csv', 'w+', newline ='') 

first_name = "John"
second_name = "Doe"

with file:     
    csv_writer = csv.writer(file) 
    for address in address_list:
        # How can I switch to a new row here?
        csv_writer.writerow(first_name + ',' + second_name + ',' + address)

print(' CSV file created with addresses ... ')

问题:
以上脚本有效。唯一的问题是所有地址都写入一行。那不符合格式。我想将每个项目写在一个新行中,以便每个新行包含 first_name 后跟 last_name 后跟地址。

另外一个问题是它会在每个项目周围加上双引号,就像这样 - “John,Doe,AddressLine1”。我怎样才能消除或忽略 "?

环境:
Python macOS Catalina 上的 3.9.0

csv 模块中的 writerow 函数将采用一个参数,该参数应为 list 类型。 像这样改变写逻辑。

with open('output.csv', 'w+', newline ='') output_file:     
    csv_writer = csv.writer(output_file) 
    for address in address_list:
        csv_writer.writerow([first_name, second_name, address])