iT邦幫忙

0

python 檔案計算問題

  • 分享至 

  • xImage

我有一個文字檔,我想計算檔案內助教的時薪
想問我應該要怎麼做才好,文字檔內容如下

以學士時薪180、碩士時薪為200基準,計算出每位助教的當月薪資。
學士的學號開頭為4,碩士則為6。

序列,姓名,學號,本月時數
1,王小明,410612123,30
2,張小偉,410611715,30
3,陳小潔,610727331,40

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
黃彥儒
iT邦高手 1 級 ‧ 2022-01-08 14:42:24

給:

txt = '''1,王小明,410612123,30
2,張小偉,410611715,30
3,陳小潔,610727331,40'''
x10 = lambda x: x*10
for i in txt.split("\n"):
    print(f"{i}, {(140 + x10(int(i[6]))) * (x10(int(i[-2])) + int(i[-1]))}")

另外貴單位也太摳了吧,160基本薪資都不到

shenglee iT邦新手 5 級 ‧ 2022-01-13 15:08:09 檢舉

如果王小明本月時數為31,這樣就會有誤了
最後一行是否應改成下式呢?
print(f"{i}, {(140 + x10(int(i[6]))) * (x10(int(i[-2])) + int(i[-1]))}")

黃彥儒 iT邦高手 1 級 ‧ 2022-01-13 20:21:43 檢舉

shenglee 忘加括號了,謝指教

/images/emoticon/emoticon25.gif

1
海綿寶寶
iT邦大神 1 級 ‧ 2022-01-08 16:24:18

參考看看合不合用
https://ithelp.ithome.com.tw/upload/images/20220108/20001787s2hhevXcE8.png

0
I code so I am
iT邦高手 1 級 ‧ 2022-01-09 14:45:10

data.csv 內容為:
序列,姓名,學號,本月時數
1,王小明,410612123,30
2,張小偉,410611715,30
3,陳小潔,610727331,40

計算:

import pandas as pd

def func(row):
    return row['本月時數'] * 180 if str(row['學號']).startswith('4') else row['本月時數'] * 200

df=pd.read_csv('data.csv')
df['小計'] = df.apply(func, axis=1)
print(df) 
0
一級屠豬士
iT邦大師 1 級 ‧ 2022-01-10 22:09:35

除了Python以外,還可以用PostgreSQL 的 file_fdw .

create extension file_fdw;

create server ithelp
foreign data wrapper file_fdw;

create foreign table ithelp0110 (
  id int
, iname text
, ino text
, whr int
) server ithelp
options (filename '/tmp/ithelp.txt', format 'csv');

select *
     , case left(ino,1)::int
         when 4 then 180 * whr
         when 6 then 200 * whr
         else 0
       end
  from ithelp0110;

https://ithelp.ithome.com.tw/upload/images/20220110/20050647HciHe8kNxt.png

我要發表回答

立即登入回答