看板 Python 關於我們 聯絡資訊
如題 文件內容如 0 1 2 3 用fread讀進來 他也是秀一樣的東西 我想存成串列 但是連\n都一起存了... 不需要\n 順便想問 readlines. readline 常用嗎? 會用在哪 自己感覺是不會用到... 還有個小問題 一定要寫成 with open xxxxx as xx 這樣的寫法嗎? 感覺很不直觀 新手學python 希望各位回答 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 117.19.160.95 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1566300552.A.17D.html ※ 編輯: eamansf96xs (117.19.160.95 臺灣), 08/20/2019 19:35:03 ※ 編輯: eamansf96xs (117.19.160.95 臺灣), 08/20/2019 19:41:07
eamansf96xs: 我現在的方法是for line 然後list.append(int(line))08/20 19:49
eamansf96xs: 但感覺不太好 因為硬要轉int 感覺降低效能08/20 19:49
s860134: 一次 read ,replace 就好了阿08/20 20:14
s860134: with open 是讓你少寫一行 f.close()08/20 20:15
eamansf96xs: 要怎麼一次replace ...08/20 20:53
eamansf96xs: 可以寫個大概嗎?08/20 20:55
papple23g: 可以用split()08/20 20:56
eamansf96xs: fread讀進來 是存在哪種資料結構 我看不出來08/20 2
0:59
eamansf96xs: 如果知道的話應該很好處理08/20 21:00
※ 編輯: eamansf96xs (117.19.160.95 臺灣), 08/20/2019 21:00:35
robert09080: 讀完資料有\n,可以使用str.split()08/20 22:59
robert09080: 檔案應該是txt所以讀進來會是str還是要轉才會變int吧08/20 23:04
eamansf96xs: 好的 感謝08/20 23:26
eamansf96xs: r大的方法 正是我要的!! 覺得python太多種寫法很08/20 23:28
eamansf96xs: 方便 但是以前寫c 有些語法真的很難接受08/20 23:28
eamansf96xs: with open就是其中一個08/20 23:28
TitanEric: with 是context manager 確保在進入function前後需要08/21 10:10
TitanEric: 做的行為 像with open就會幫你關檔08/21 10:10
※ 編輯: eamansf96xs (117.19.160.95 臺灣), 08/21/2019 10:41:19