When writing to a csv file with python, a story that I made a mistake and did not meet the delivery date

environment

MAC python 2.7 series

Thing you want to do

I wanted to write a new line to a locally existing csv file. For some reason this didn't work, so I'll summarize it.

code

import csv

c = csv.writer(open(path, 'w'))
lst = ['a','b','c','d']
lst.append('\n')
c.writerow(lst)

Looking at the Japanese source site, it seems to work fine. There were times when it worked and times when I tried it, but in most cases it was out!

solution

import csv

c = csv.writer(open(path, 'a'))
lst = ['a','b','c','d']
lst.append('\n')
c.writerow(lst)

I changed the mode of open () to `'w'->'a'`. It seems that'a' is fine because I just wanted to write to an existing file.

Lack of google power that could not find Japanese source

There was a detailed description in site here.

If only the write process is performed without performing the read process at all, the data will be overwritten from the beginning of the file (as it is), but if the written length is shorter than the original data, the written data will end. The original data remains behind.

I see ~

result

The delivery date was not in time. The reason I didn't make it in time was that the execution time was long, so I fell asleep with the script running.

When I woke up in the morning, there was only one line in the csv file, so I screamed and went to bed again.

Recommended Posts

When writing to a csv file with python, a story that I made a mistake and did not meet the delivery date
A story that I was addicted to when I made SFTP communication with python
A story that didn't work when I tried to log in with the Python requests module
I tried to touch the CSV file with Python
A story that I did not know how to load a mixin when making a front with the django app [Beginners learn python with a reference book in one hand]
A story that stumbled when I made a chatbot with Transformer
I made a library that adds docstring to a Python stub file.
I made a configuration file with Python
[Django] I made a field to enter the date with 4 digit numbers
I made a program to check the size of a file in Python
How to read a CSV file with Python 2/3
I want to write to a file with Python
A story that did not end with exit when turning while with pipe input
A Python script that reads a SQL file, executes BigQuery and saves the csv
I thought it was the same as python, and I was addicted to the problem that the ruby interpreter did not start.
The story that had nothing to do with partitions when I did disk backup with dd for the first time
I made a system that automatically decides whether to run tomorrow with Python and adds it to Google Calendar.
I made a LINE BOT with Python and Heroku
A memo that I touched the Datastore with python
A story that failed when trying to remove the suffix from the string with rstrip
A story that got stuck when trying to upgrade the Python version on GCE
I made a program to convert images into ASCII art with Python and OpenCV
There is a pattern that the program did not stop when using Python threading
Try to find the probability that it is a multiple of 3 and not a multiple of 5 when one is removed from a card with natural numbers 1 to 100 using Ruby and Python.
I made a toolsver that spits out OS, Python, modules and tool versions to Markdown
[Python] Read the csv file and display the figure with matplotlib
A story that makes it easy to estimate the living area using Elasticsearch and Python
I made a class to get the analysis result by MeCab in ndarray with python
The story that `while queue` did not work in python
What I did to welcome the Python2 EOL with confidence
A story that went missing when I specified a path starting with a tilde (~) in python open
I made a program to collect images in tweets that I liked on twitter with Python
I made a server with Python socket and ssl and tried to access it from a browser
I tried to make a generator that generates a C # container class from CSV with Python
A program that failed when trying to create a linebot with reference to "Dialogue system made with python"
I also tried to imitate the function monad and State monad with a generator in Python
I tried to divide the file into folders with Python
Workaround for the problem that sys.argv is not passed when executing a Python script with only the file name in Python2.7 on Windows
When I made a recommended game search with YoutubeDataAPIv3 and fastText, I found a game that seemed to be interesting that I had never known before.
I want to output while converting the value of the type (e.g. datetime) that is not supported when outputting json with python
When I tried to use pip with python, I was told that XML_SetHashSalt could not be found.
I made something with python that NOW LOADING moves from left to right on the terminal
I made a simple circuit with Python (AND, OR, NOR, etc.)
The story that the version of python 3.7.7 was not adapted to Heroku
[Python3] I made a decorator that declares undefined functions and methods.
I made a package that can compare morphological analyzers with Python
I want to use a wildcard that I want to shell with Python remove
A story about adding a REST API to a daemon made with Python
A story that struggled to handle the Python package of PocketSphinx
[Python] A memo that I tried to get started with asyncio
I made a Nyanko tweet form with Python, Flask and Heroku
I tried to make a periodical process with Selenium and Python
I made a shuffle that can be reset (reverted) with Python
I wanted to solve the ABC164 A ~ D problem with Python
The story that 2D list replacement did not work in python
I made a program that automatically calculates the zodiac with tkinter
The story of making a module that skips mail with python
[python] I made a class that can write a file tree quickly
[Python] Write to csv file with Python
Output to csv file with Python
I made a fortune with Python.