
from Scratch
127
for stock, price in todays
_
prices.items():
csv
_
writer.writerow([stock, price])
데이터 필드 자체에 쉼표가 포함되어 있더라도
csv.writer
는 올바른 방식으로
처리해 줄 것이다. 하지만 구분자가 있는 파일을 생성해 주는 코드를 직접 만들
어 사용한다면 문제가 발생할 수도 있다. 예를 들어 다음과 같은 코드를 만들었
다면
results = [["test1", "success", "Monday"],
["test2", "success, kind of", "Tuesday"],
["test3", "failure, kind of", "Wednesday"],
["test4", "failure, utter", "Thursday"]]
#
이렇게
하면
안
된다
.
with open('bad
_
csv.txt', 'w') as f:
for row in results:
f.write(",".join(map(str, row))) #
필드
안에
쉼표가
있을
수도
있다
!
f.write("\n") #
필드
안에
새로운
줄이
있을
수도
있다
!
결과는 아무도 알아볼 수 없는
.csv
파일이 만들어질 것이다.
test1,success,Monday
test2,success, kind of,Tuesday ...