1 回答

TA贡献1890条经验 获得超9个赞
想要编写的代码需要以wr某种方式访问变量的值。您没有告诉我们该代码真正想要做什么,但作为起点,这里是修改示例的一种方法。
import csv
def start():
NewCSV = input('Name of csv: ')
with open(NewCSV + '.csv', 'w') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
Code_that_writes_in_CSV(wr)
def Code_that_writes_in_CSV(handle):
handle.writerow([''])
更常见的安排是将所有 CSV 处理保留在单个函数中,并且可能在单独的函数中执行一些计算。
def write_to_csv(filename):
with open(filename, 'w') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
for row in code_that_calculates_a_row_at_a_time():
wr.writerow(row)
一次计算一行的代码可以是行return的列表,或者一次可以是yield一行。
添加回答
举报