Python中文
首页
教程
问答
标签
搜索
登录
注册
从.txt读取3列数据到lis
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>你好,我有个问题。我有下面的文本文件(3列和空格之间)。我想把每一列(tt,ff,ll)插入它自己的列表(time,l,f)。在</p> <p>文本文件:</p> <pre><code> 0.0000000000000000 656.5342434532456082 0.9992059165961109 1.0001828508431749 656.5342334512754405 1.0009810769697651 2.0003657016863499 656.5342259805754566 0.9989386155502871 3.0005485525295246 656.5342339081594218 1.0005032672779635 4.0007314033726997 656.5342356101768928 0.9996101946453564 5.0009142542158749 656.5342236489159404 0.9986884414684027 6.0010971050590491 656.5342474828242985 1.0001061182847479 7.0012799559022243 656.5342355894648563 1.0003982380731031 8.0014628067453994 656.5342256832242356 0.9993176599964499 9.0016456575885737 656.5342218575017341 0.9999117456245585 10.0018285084317498 656.5342408970133192 1.0000973751521087 11.0020113592749240 656.5342243211601954 0.9997189612768125 12.0021942101180983 656.5342320396634932 0.9997487346699927 13.0023770609612743 656.5342291293554808 0.9991986731183715 </code></pre> <p>但我想要以下输出:</p> ^{pr2}$ <p>尝试代码:</p> <pre><code>from numpy import * def read_file(filename): time = [] # list time f = [] # ... l = [] # ... infile = open(filename, "r") # reads file for line in infile: # each line in txt file numbers = line.split() # removes the " " tt = numbers[0] # 1st column? ff = numbers[1] # 2nd column? ll = numbers[2] # 3rd column? time.append(tt) # Inserts 1st column(tt) into list(time) f.append(ff) # ... l.append(ll) # ... return time,f,l # return lists txt1 =read_file("1.txt") # calls function print txt1 # print return values </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>使用numpy的<code>loadtxt</code>函数</p> <pre><code>text_array = np.loadtxt('my_file.txt') time = text_array[:, 0] l = text_array[:, 1] f = text_array[:, 2] </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
jupyter运行一个旧的pytorch版本
4 回答
Jupyter运行不同版本的卸载库?
5 回答
Jupyter运行指定的键盘快捷键
9 回答
Jupyter通过.local文件“逃逸”virtualenv。我该如何缓解这种情况?
3 回答
Jupyter重新加载自定义样式
8 回答
Jupyter错误:“没有名为Jupyter_core.paths的模块”
4 回答
jupyter错误:无法在随机林中将决策树视为png
10 回答
Jupyter错误'内核似乎已经死亡,它将自动重新启动'为一个给定的代码块
5 回答
Jupyter错误地用阿拉伯语和字母数字元素显示Python列表
3 回答
Jupyter隐藏数据帧索引,但保留原始样式
8 回答
Jupyter集线器:启动器中出现致命错误。。。系统找不到指定的文件
8 回答
Jupyther中相同值的相同哈希,但导出到Bigquery时不相同
5 回答
Jupy上Python的读/写访问问题
4 回答
jupy上没有模块cv
1 回答
Jupy上的排序错误
5 回答
Jupy中bqplot图形的紧凑布局
4 回答
Jupy中matplotlib plot的连续更新
7 回答
Jupy中Numpy函数的文档
4 回答
Jupy中Pandas的自动完成问题
9 回答
jupy中Qt后端的Matplotlib动画
9 回答