瀏覽分類:

Python

Python文字轉語音(Text TO Speech)

text_file_to_speech.py

安裝

pip install gtts

文字檔案轉語音

# Import the Gtts module for text  
# to speech conversion 
from gtts import gTTS 
  
# import Os module to start the audio file
import os 

fh = open("test.txt", "r")
myText = fh.read().replace("\n", " ")

# Language we want to use 
language = 'en'

output = gTTS(text=myText, lang=language, slow=False)

output.save("output.mp3")
fh.close()

# Play the converted file 
os.system("start output.mp3")

文字轉語音

# Import the Gtts module for text  
# to speech conversion 
from gtts import gTTS 
  
# import Os module to start the audio file
import os 
  
mytext = 'Convert this Text to Speech in Python'
  
# Language we want to use 
language = 'en'
myobj = gTTS(text=mytext, lang=language, slow=False) 
myobj.save("output.mp3") 
  
# Play the converted file 
os.system("start output.mp3") 

如何使用Python讀寫SPSS的檔案

使用Pandas

import pandas as pd

df =pd.read_spss('fileName.sav')
df.tail()
cols = ['ID', 'Day', 'Age', 'Response', 'Gender']
df = pd.read_spss('fileName.sav', usecols=cols)
df.head()

使用Pyreadstat

#讀取
import pyreadstat

df, meta = pyreadstat.read_sav('fileName.sav')

type(df)
df.head()#寫入
pyreadstat.write_sav(df, 'fileName.sav')

Python Scipy科學計算學習筆記

參考資料

資料分析與視覺化 ( 5 )-資料好幫手 Pandas 與 Scipy 基本操作

安裝

 pip install scipy

線性代數

import numpy as np
from scipy import linalg

# 解二元一次線性方程式
# 2x + 2y = 5
# 4x + 3y = 6

# 係數
A = np.array([[2,2],[4,3]])

# 答案
B = np.array([[5],[6]])

# 解方程式
ans = linalg.solve(A,B)

"answer: x=%s, y=%s" % (ans[0][0], ans[1][0])     

積分

from scipy import integrate

# 對 x 三次方從 0 到 2 進行定積分
ans= integrate.quad(lambda x: x**3, 0,2)

ans[0]

傅立葉轉換

from scipy.fftpack import fft, ifft

x = np.array([1.0, 1.5, 1.0, -1.0, 1.5])
y = fft(x)
y