我正在尝试使用python中的CSV文件创建数据库。我想使用已经创建的函数写入csv文件,并在csv.writerow()函数中调用它。
谢谢。
filename = 'tag_storage'
with open(filename, 'a', newline='') as csvfile:
fieldnames = ['name', 'artists', 'albums', 'tracks' ,'wiki', 'shouts']
writer = csv.DictWriter(csvfile, fieldnames = fieldnames)
writer.writerow({'name' : pathTuple[2],
'artists' : gather_tag_artists(page_url),
'albums' : gather_tag_albums(page_url),
'tracks' : gather_tag_tracks(page_url),
'wiki' : gather_tag_wiki(page_url),
'shouts' : gather_tag_shouts(page_url)})
csvfile.close()
### Reproducible Example
def csvWrite():
filename = 'storage'
with open(filename, 'a', newline='') as csvfile:
fieldnames = ['field1', 'field2']
writer = csv.DictWriter(csvfile, fieldnames = fieldnames)
writer.writerow({'field1' : func1(),
'field2' : func2()})
def func1():
return('teststring1')
def func2()
return('teststring2')
我希望函数可以运行并执行writerow()来恢复返回的值,但是什么也没有存储。
EDIT1:在writerow()中调用的所有函数都应产生字符串。当我打开文件时,标题在那里没有数据。在运行代码之前,我是手动输入标题的。
EDIT2:我提供了一个可重复的示例