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

后端 未结 2 653
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 08:11

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

相关标签:
2条回答
  • 2020-12-30 08:36

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

    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.

    0 讨论(0)
  • 2020-12-30 08:45

    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.
    0 讨论(0)
提交回复
热议问题