# 'w'about
('test.csv', 'w', encoding='utf-8')
Notation | Overview |
---|---|
r | Read |
w | Write new |
a | Write in postscript |
r+ | An error will occur if the additional write / read file does not exist. |
w+ | Create if there are no new write and read files |
a+ | Write and read additional notes, create if there is no file |
hello.py
#
with open('test.csv', 'w', encoding='utf-8') as f:
f.write('Hello World\n')
print('I wrote it.')
-----result------
I wrote it.
test.csv
Hellor World
lang.py
lang = ['SQL',
'Java',
'Ruby',
'PHP',
'Python']
with open('lang.csv', 'w', encoding = 'utf-8') as f:
for item in lang:
f.write(item + '\n')
print('I wrote it.')
lang.csv
SQL
Java
Ruby
PHP
Python
time.py
from datetime import datetime
now = datetime.now()
#print(now) 2020-10-27 19:56:15.019242
str_now = f'{now:%Y/%m/%d %H:%M}'
#print(str_now) 2020/10/27 19:56
with open('time.csv', 'w', encoding='utf-8') as f:
f.write(str_now + ' -holiday\n')
print('I wrote it.')
time.csv
2020/10/27 20:00 -holiday
Recommended Posts