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问题
如何在Excel中读取公式并将其转换为Python中的计算?
8 回答
如何在excel中读取嵌入的excel,并将嵌入文件中的信息存储在主excel文件中?
4 回答
如何在Excel中返回未知列长度的非空顶行列值?
2 回答
如何在excel中选择数据列?
10 回答
如何在Excel中通过脚本自动为一列中的所有单元格创建公共别名
4 回答
如何在excel中高效格式化范围AttributeError:“tuple”对象没有属性“fill”
9 回答
如何在excel单元格中编写python函数
7 回答
如何在excel单元格中自动执行此python代码?
2 回答
如何在excel工作表中创建具有相应值的新列
8 回答
如何在Excel工作表中复制条件为单元格颜色的python数据框?
7 回答
如何在Excel工作表中循环
10 回答
如何在excel工作表中打印嵌套词典?
2 回答
如何在excel工作表中绘制所有类的继承树?
3 回答
如何在Excel工作表中自动调整列宽?
5 回答
如何在excel工作表中追加并进一步处理
3 回答
如何在excel工作表之间进行更改?
9 回答
如何在excel或csv上获取selenium数据?
7 回答
如何在Excel或Python中将正确的值赋给正确的列
5 回答
如何在excel或python中提取单词周围的文本?
9 回答
如何在excel或python中转换来自Jira的3w 1d 4h的fromat数据?
10 回答