作者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/cn.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