在python csv文件中写一个列表,每个列表一个新行 [英] Write a list in a python csv file, one new row per list

查看:464
本文介绍了在python csv文件中写一个列表,每个列表一个新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下源代码,我想在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()

问题是在每次运行代码时,在换行符中插入list1。



在matlab中很容易,我只需要使用dlmwrite和'-append' / p>

但是如何在Python中执行此操作?

解决方案

附加模式。

  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 )






更多档案打开模式



请尝试以下操作: -

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






link



模式:说明


  1. r :打开只读文件。文件指针放在文件的开头。这是默认模式。

  2. rb :打开文件仅以二进制格式阅读。文件指针放在文件的开头。这是默认模式。

  3. r + :打开用于阅读和写入的文件。文件指针将位于文件的开头。

  4. rb + :打开以二进制格式读取和写入的文件。文件指针将位于文件的开头。

  5. w :打开只能写入的文件。如果文件存在,则覆盖文件。如果文件不存在,则创建一个新文件以进行写入。

  6. wb :打开仅以二进制格式写入的文件。如果文件存在,则覆盖文件。如果文件不存在,则创建一个新文件以进行书写。

  7. w + :打开用于写入和阅读的文件。如果现有文件存在,则覆盖现有文件。如果文件不存在,则创建一个新文件进行读写。

  8. wb + :打开以二进制格式写入和读取的文件。如果现有文件存在,则覆盖现有文件。如果文件不存在,则创建一个新文件进行读写。

  9. a :打开要附加的文件。如果文件存在,文件指针在文件的结尾。也就是说,文件处于追加模式。如果文件不存在,它会创建一个新文件以供书写。

  10. ab :打开要以二进制格式附加的文件。如果文件存在,文件指针在文件的结尾。也就是说,文件处于追加模式。
  11. a + :打开一个文件,用于追加和阅读。如果文件存在,文件指针在文件的结尾。文件在追加模式下打开。如果文件不存在,它会创建一个新文件进行读写。
  12. ab + :以二进制格式打开一个文件。如果文件存在,文件指针在文件的结尾。文件在追加模式下打开。如果文件不存在,则会创建一个新文件进行读写。


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:

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.

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

But how to do this in Python?

解决方案

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.

这篇关于在python csv文件中写一个列表,每个列表一个新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆