-1

我有几个数据框要附加到 python 中的 csv 文件。我正在使用以下代码来实现这一点,但还没有运气。

  import csv as writer
  import pandas as pd

def append_list_as_row(list_of_elem):
        # Open file in append mode
        with open('my_file.csv', 'a+', newline='') as write_obj:
            # Create a writer object from csv module
            csv_writer = writer(write_obj)
            # Add contents of list as last row in the csv file
            csv_writer.writerow(list_of_elem)
    append_list_as_row(new_data_frame)

抛出以下错误:

TypeError: 'module' object is not callable

任何人都可以帮我解决这个问题吗?

4

1 回答 1

0

我想你忘记在csvwriter初始化时使用 csv.writer。尝试这个

def append_list_as_row(list_of_elem):
# Open file in append mode
    with open('my_file.csv', 'a+', newline='') as write_obj:
        # Create a writer object from csv module
        csv_writer = writer(write_obj)
        # Add contents of list as last row in the csv file
        csv_writer.writerow(list_of_elem)
        csv_writer.close()
    append_list_as_row(new_data_frame)

另一个问题是所有框架都有一条线吗?如果不是,您可以使用 for into with。像这样

for line in list_of_elem :
    csv_writer.writerow(line)

而且您忘记使用关闭文件。

于 2020-09-07T23:22:00.640 回答