Printed copies of Python for Informatics are available for $10 or less from Amazon and $2 or less on Kindle:

  • Spanish: Amazon / Kindle (Python para Informáticos: Explorando la información)
  • Korean: Amazon ( 정보교육을 위한 파이썬: 정보탐색을 통한 데이터 과학자로의 여정 )

Here are free copies of the book in various languages:

Additional English versions:

You can download all of the sample Python code from the book from http://www.pythonlearn.com/code.zip, or individual files from the directory http://www.pythonlearn.com/code/

All of the book materials are available under a Creative Commons Attribution-NonCommercial 3.0 Unported License. The slides, audio, assignments, auto grader and all course materials other than the book are available from oer.pr4e.org under the more flexible Creative Commons Attribution 3.0 Unported License. If you are curious as to why the "NC" variant of Creative Commons was used, see Appendix D of the textbook or search through my blog posts for the string "copyright".


강좌링크

https://www.coursera.org/learn/python-data

https://www.coursera.org/learn/python-network-data

https://www.coursera.org/learn/python-databases

블로그 이미지

NCookie

,

네이버 음성 합성 API를 사용할 일이 있어서 문서를 찾아보게 되었습니다.


https://developers.naver.com/docs/labs/tts


음성 파일을 얻어오는 방식은 간단한데 지정된 포맷에 따라 http 통신을 POST 방식으로 Request하고 Response에 있는 파일을 로컬에 저장하면 됩니다.




curl 명령어를 통해서 API를 요청하는 방법은 다음과 같습니다.



curl "https://openapi.naver.com/v1/voice/tts.bin" \
	-d "speaker=mijin&speed=0&text=만나서 반갑습니다." \
	-H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" \
	-H "X-Naver-Client-Id: {애플리케이션 등록 시 발급받은 client id 값}" \
	-H "X-Naver-Client-Secret: {애플리케이션 등록 시 발급받은 client secret 값}" -v \
		> out.mp3




위의 명렁어를 쉘 스크립트에서 간단하게 작성했습니다. 

#!/bin/sh

argc=$#
argv0=$0
argv1=$1
argv2=$2

curl "https://openapi.naver.com/v1/voice/tts.bin" -d "speaker=mijin&speed=0&text=$argv0" -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "X-Naver-Client-Id: $argv1" -H "X-Naver-Client-Secret: $argv2" -v > out.mp3

chmod 775 out.mp3


argv0=$0 이 부분은 스크립트를 실행할 때 인자를 받기 위해 변수를 저장하는 코드입니다. 


리다이렉션을 통해 out.mp3 파일에 음성이 저장되었고 파일 접근 권한을 rwxrwxr-x 로 변경하였습니다.




이를 파이썬에서 실행해봅시다.


파이썬에는 외부의 명렁어를 실행시킬 수 있는 subprocess라는 모듈이 있습니다. 이 글에서는 call 이라는 메소드만 사용하였습니다.


import subprocess
subprocess.call(['./tts.sh', "hello world", "{어플리케이션 clinet id}", "{어플리케이션 clinet secret}"])






블로그 이미지

NCookie

,

  아마 파이썬 스크립트를 실행할 때 커맨드 라인에서 인자를 전달하기 위해서 argparse 모듈을 사용해 본적이 있으실 것입니다. 그런데 python 2.7에서는 유니코드 문자열을 전달하려고 하면 자꾸 거부를 합니다. (파이썬 2.x 버전은 문자열 인코딩이 제일 짜증나는 것 같습니다.)


UnicodeDecodeError: 'ascii' codec can't decode byte 0xb9 in position 0: ordinal not in range(128)


  이 때 해결할 수 있는 방법이 있습니다. 바로 sys.getfilesystemencoding() 이라는 함수를 이용하는 것입니다, 예제 코드를 보겠습니다.


# -*- coding: utf-8 -*-

import argparse
import unicodecsv as csv
import sys


def commandline_arg(byte_string):
unicode_string = byte_string.decode(sys.getfilesystemencoding())
return unicode_string

parser = argparse.ArgumentParser()
parser.add_argument("-i", dest="input_file_name", default=None,
help="input file name(s) for CSV data to get data")
parser.add_argument("-target", dest="target_name", default=None,
type=commandline_arg,
help="target name is data for parsing")
parser.add_argument("-o", dest="output_file_name", default="data_set.csv",
type=commandline_arg)

args = parser.parse_args()

with open('input_data_set.csv', 'r') as r:
reader = csv.reader(r) # Here your csv file
lines = [l for l in reader if l[3] == args.target_name]

with open(args.output_file_name, 'wb') as w:
writer = csv.writer(w)
writer.writerows(lines)


 위와 같이 comandline_arg(byte_string) 함수를 선언하고 args type에 함수 이름을 넣어주면 됩니다.

블로그 이미지

NCookie

,