最後修改於 2025 / 6 / 11 by CML
# -- coding: utf-8 --
"""
Created on Mon May 2 16:14:50 2022
906 字串替換
@author: User
"""
f_name = input() # 請求用戶輸入文件名稱
str_old = input() # 請求用戶輸入要替換的舊字符串
str_new = input() # 請求用戶輸入新的字符串
file = open(f_name, 'r+') # 打開指定的文件,讀取和覆蓋模式
data = file.read() # 讀取文件內容
print("==== Before the replacement ====")
print(data) # 打印原始數據
print("==== After the replacement ====")
data = data.replace(str_old, str_new) # 進行字符串替換
print(data) # 打印替換後的數據
file.seek(0) # 將文件指針移到開始位置
file.write(data) # 將替換後的數據寫回文件
file.close() # 關閉文件
# -- coding: utf-8 --
"""
Created on Mon May 2 16:20:07 2022
907 詳細資料顯示
@author: User
"""
f_name = input() # 請求用戶輸入文件名稱
f_line = f_word = f_char = 0 # 初始化行數、詞數和字符數
# 使用 with 語句開啟文件
with open(f_name, 'r') as file: # 以讀取模式打開文件
for line in file: # 遍歷文件的每一行
f_line += 1 # 行數增加
f_word += len(line.split()) # 計算詞數
f_char += sum(len(c) for c in line.split()) # 計算每個詞的字符長度並累加
print('%d line(s)' % f_line) # 打印行數
print('%d word(s)' % f_word) # 打印詞數
print('%d character(s)' % f_char) # 打印字符數
"""
Created on Wed Jun 11 20:31:04 2025
908 字元數計算
@author: CSU-MIS
"""
f_name = input('請輸入人要讀取的檔案名稱:') # 請求用戶輸入文件名稱
n = int(input('字元出現次數:')) # 請求用戶輸入要查找的出現次數
countDict = {} # 用於存儲單詞及其出現次數的字典
f_word = [] # 用來存儲出現次數等於 n 的單詞
with open(f_name, 'r', encoding='utf-8') as file: # 打開指定的文件
for line in file: # 對於文件中的每一行
for w in line.split(): # 將每行按空格分割成單詞
if w in countDict: # 如果單詞已在字典中
countDict[w] += 1 # 增加該單詞的計數
else: # 如果單詞不在字典中
countDict[w] = 1 # 新增單詞並設置計數為1
print('countDict:', countDict) # 打印單詞和其出現次數的字典
print('Len of countDict:', len(countDict)) # 打印獨特單詞的數量
for word in countDict: # 遍歷字典中的所有單詞
if countDict[word] == n: # 如果單詞的計數等於 n
f_word.append(word) # 將該單詞添加到 f_word 列表中
f_word.sort() # 對出現次數等於 n 的單詞進行排序
for ans in f_word: # 遍歷存儲結果的 lista
print('ans', ans) # 打印所有符合條件的單詞
# pip install pandas
# 使用 pandas 讀取 csv 檔案
import pandas as pd
file_path = 'sales.csv'
# 嘗試使用 Big5 編碼讀取
df = pd.read_csv(file_path, encoding='big5')
print(df.head())
銷售紀錄表 | 2025/01 | 2025/02 | 2025/03 | 2025/04 | 2025/05 | 2025/06 |
---|---|---|---|---|---|---|
台北 | 8000 | 9000 | 10000 | 11000 | 12000 | 13000 |
台中 | 8000 | 8200 | 8400 | 8600 | 8800 | 9000 |
高雄 | 9000 | 9050 | 9100 | 9150 | 9200 | 9250 |
import pandas as pd
import matplotlib.pyplot as plt
# 設定 CSV 檔案路徑
file_path = 'sales.csv'
# 讀取 CSV (Big5 編碼)
df = pd.read_csv(file_path, encoding='big5')
print("原始資料:")
print(df)
# 將資料轉換為長格式 (地區、月份、銷售)
df_melted = df.melt(id_vars=[df.columns[0]], var_name='Month', value_name='Sales')
df_melted.rename(columns={df.columns[0]: 'Region'}, inplace=True)
# 將月份轉換為 datetime 格式
df_melted['Month'] = pd.to_datetime(df_melted['Month'] + '/01', format='%Y/%m/%d')
# 繪製圖表
plt.figure(figsize=(10, 5))
# 每個地區畫一條線
for region in df_melted['Region'].unique():
region_data = df_melted[df_melted['Region'] == region]
plt.plot(region_data['Month'], region_data['Sales'], marker='o', label=region)
# 設定圖表標題及坐標軸
plt.title('Monthly sales trend chart by region', fontsize=14)
plt.xlabel('Months', fontsize=12)
plt.ylabel('Sales Amounts', fontsize=12)
plt.xticks(rotation=45)
plt.grid()
plt.legend()
plt.tight_layout()
plt.show()