在 python csv 文件中写一个列表,每个列表一个新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28277150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:02:28  来源:igfitidea点击:

Write a list in a python csv file, one new row per list

pythonlistcsv

提问by mad

I have the following source code, where I am trying to write a list in a csv file. I need every new list to be written in a new line of this csv file. The source code is the following:

我有以下源代码,我试图在 csv 文件中编写一个列表。我需要在这个 csv 文件的新行中写入每个新列表。源代码如下:

import csv
list1=[55,100,'dir1/dir2/dir3/file.txt',0.8]

resultFile = open("output.csv",'wa')
wr = csv.writer(resultFile, dialect='excel')
wr.writerow(list1)
resultFile.close()

The problem is that it doesn't insert list1 in a newline every time i run the code.

问题是每次我运行代码时它都不会在换行符中插入 list1 。

In matlab that would be easy, I just need to use dlmwrite with '-append' parameter.

在matlab中这很容易,我只需要使用带有'-append'参数的dlmwrite。

But how to do this in Python?

但是如何在 Python 中做到这一点呢?

采纳答案by Vivek Sable

Open file in append mode.

以追加模式打开文件。

import csv
list1=[58,100,'dir1/dir2/dir3/file.txt',0.8]

with open("output.csv", "a") as fp:
    wr = csv.writer(fp, dialect='excel')
    wr.writerow(list1)


More on file open modes

有关文件打开模式的更多信息

try following:-

尝试以下:-

>>> with open('test1','wb') as f: f.write('test')
... 
>>> with open('test1','ab') as f: f.write('koko')
... 
>>> with open('test1','rb') as f: f.read()
... 
'testkoko'
>>> with open('test1','wa') as f: f.write('coco')
... 
>>> with open('test1','rb') as f: f.read()
... 
'coco'
>>> 


From this link

从这个链接

Modes: Description

模式:描述

  1. r: Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
  2. rb: Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
  3. r+: Opens a file for both reading and writing. The file pointer will be at the beginning of the file.
  4. rb+: Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file.
  5. w: Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
  6. wb: Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
  7. w+: Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
  8. wb+: Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
  9. a: Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
  10. ab: Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
  11. a+: Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
  12. ab+: Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
  1. r:以只读方式打开文件。文件指针位于文件的开头。这是默认模式。
  2. rb: 以二进制格式打开一个只读文件。文件指针位于文件的开头。这是默认模式。
  3. r+:打开文件进行读写。文件指针将位于文件的开头。
  4. rb+:以二进制格式打开文件进行读写。文件指针将位于文件的开头。
  5. w: 打开一个只用于写入的文件。如果文件存在,则覆盖该文件。如果文件不存在,则创建一个新文件进行写入。
  6. wb:打开一个文件只以二进制格式写入。如果文件存在,则覆盖该文件。如果文件不存在,则创建一个新文件进行写入。
  7. w+:打开文件进行读写。如果文件存在,则覆盖现有文件。如果文件不存在,则创建一个新文件进行读写。
  8. wb+:以二进制格式打开文件进行读写。如果文件存在,则覆盖现有文件。如果文件不存在,则创建一个新文件进行读写。
  9. a:打开一个文件进行追加。如果文件存在,则文件指针位于文件末尾。也就是说,文件处于追加模式。如果文件不存在,它会创建一个新文件用于写入。
  10. ab:打开一个文件以二进制格式追加。如果文件存在,则文件指针位于文件末尾。也就是说,文件处于追加模式。如果文件不存在,它会创建一个新文件用于写入。
  11. a+:打开文件进行追加和读取。如果文件存在,则文件指针位于文件末尾。文件以追加模式打开。如果文件不存在,它会创建一个新文件进行读写。
  12. ab+:以二进制格式打开一个文件进行追加和读取。如果文件存在,则文件指针位于文件末尾。文件以追加模式打开。如果文件不存在,它会创建一个新文件进行读写。

回答by Sergey Solod

If you use Python 3.x then change your code:

如果您使用 Python 3.x,请更改您的代码:

import csv

list1 = [58,100,'dir1/dir2/dir3/file.txt',0.8]

with open("output.csv", "a", newline='') as fp:
    wr = csv.writer(fp, dialect='excel')
    wr.writerow(list1)

Adding newline=''can help you to avoid getting extra new lines, empty rows in between two rows with data.

添加newline=''可以帮助您避免获得额外的新行,两行之间的空行和数据。