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