Python 2.7 csv
は、'b'
の書き込みモード(Python 3の場合は'w'
)が好きです。
例:クイック:writer()
import csv
# read current rows in csv and return reader object
def read(_f):
with open(_f, 'rb') as f:
reader = csv.reader(f, delimiter=',')
return reader
# writes the reader object content
# then writes the new content to end
def write(_f, _reader, _adding):
with open(_f, 'wb') as f:
writer = csv.writer(f, delimiter=',')
for row in _reader:
writer.writerow(row)
for row in _adding:
writer.writerow(row)
strings = []
s1 = 'abcd'
s2 = 'wxyz'
strings.append(s1)
strings.append(s2)
csvf = r"C:\path\to\my\file.csv"
content = read(csvf)
write(csvf, content, strings)
例を供給するために、行のリストを構築するためにreader()
の使用:例
import csv
strings = []
s1 = 'abcd'
s2 = 'wxyz'
strings.append(s1)
strings.append(s2)
csvf = r"C:\path\to\my\file.csv"
with open(csvf, 'wb') as f:
w = csv.writer(f, delimiter=',')
for s in strings:
w.writerow(s)
を提出するstring
の事前構築されたリスト追記
の
import csv
strings = []
s1 = 'abcd'
s2 = 'wxyz'
strings.append(s1)
strings.append(s2)
csvf = r"C:\path\to\my\file.csv"
with open(csvf, 'ab') as f:
writer = csv.writer(f, delimiter=',')
for s in strings:
writer.writerow(s)
参照:Python 2.xで
、reader()
とwriter()
オブジェクトは、開口時'b'
フラグを必要としました。これは、モジュールが回線終了を処理する方法の結果でした。
reader()
とwriter()
のオブジェクトをnewline=''
で開くように変更しました。しかし、回線終端はまだ処理されます。
また、this投稿とthat投稿があります。
'str'のような組み込み関数の後にオブジェクトの名前を決して入れてはいけません –
スクリプト全体を投稿できますか?少しの情報が欠落しているようです –
あなたのコメントに基づいて、ここの完全な画像。私はいくつかのコードが不足していると思う... – pstatix