Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在Python中将行附加到csv_Python_Csv_Append_Jupyter_Write - Fatal编程技术网

无法在Python中将行附加到csv

无法在Python中将行附加到csv,python,csv,append,jupyter,write,Python,Csv,Append,Jupyter,Write,我正在使用以下函数向现有csv文件追加一行。当我在代码中调用此函数时,它不会给我任何错误,但它不会向文件追加任何内容。几天前,同样的函数和同样的代码似乎工作得非常好。当我重新启动Anaconda/Jupyter笔记本电脑时,我是否必须执行任何特定步骤才能使此功能正常工作 def append_list(file_name, list_of_elem): # Open file in append mode with open(file_name, 'a+', newline='') as wri

我正在使用以下函数向现有csv文件追加一行。当我在代码中调用此函数时,它不会给我任何错误,但它不会向文件追加任何内容。几天前,同样的函数和同样的代码似乎工作得非常好。当我重新启动Anaconda/Jupyter笔记本电脑时,我是否必须执行任何特定步骤才能使此功能正常工作

def append_list(file_name, list_of_elem):
# Open file in append mode
with open(file_name, '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)
    # List of strings
非常感谢

试试这个。会有用的

import csv  
 
def append_list(file_name, list_of_elem):
with open(file_name, 'a', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(list_of_elem)

上下文管理器存在缩进问题。试试这个:

def append_list(file_name, list_of_elem):
     with open(file_name, 'a+', newline='') as write_obj:
         write_obj.writestr(list_of_elem)

请使您的代码有效(缩进在
append\u list
函数中,缩进的
行应该是…
行)对不起,我更正了缩进,它仍然不起作用