将文本文件解析为python中的列表 [英] Parsing a text file into a list in python

查看:196
本文介绍了将文本文件解析为python中的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python完全陌生,我正在尝试读取包含单词和数字的组合的txt文件.我可以很好地读取txt文件,但是我正在努力将字符串转换成可以使用的格式.

I'm completely new to Python, and I'm trying to read in a txt file that contains a combination of words and numbers. I can read in the txt file just fine, but I'm struggling to get the string into a format I can work with.

import matplotlib.pyplot as plt
import numpy as np
from numpy import loadtxt

f= open("/Users/Jennifer/Desktop/test.txt", "r")

lines=f.readlines()

Data = []

list=lines[3]
i=4
while i<12:
        list=list.append(line[i])
        i=i+1

print list

f.close()

我想要一个包含第3-12行(从0开始)中所有元素的列表,它是所有数字.当我打印行[1]时,我从该行获取数据.当我打印行或打印行[3:12]时,我得到的每个字符都以\ x00开头.例如,单词"Plate"变为:['\ x00P \ x00l \ x00a \ x00t \ x00e.使用lines = [f中的line的line.strip()]可获得相同的结果.当我尝试在上面的while循环中将各行放在一起时,出现错误"AttributeError:'str'对象没有属性'append'."

I want a list that contains all the elements in lines 3-12 (starting from 0), which is all numbers. When I do print lines[1], I get the data from that line. When I do print lines, or print lines[3:12], I get each character preceded by \x00. For example, the word "Plate" becomes: ['\x00P\x00l\x00a\x00t\x00e. Using lines = [line.strip() for line in f] gets the same result. When I try to put individual lines together in the while loop above, I get the error "AttributeError: 'str' object has no attribute 'append'."

如何从txt文件到列表中选择行?非常感谢!!!

How can I get a selection of lines from a txt file into a list? Thank you so much!!!

txt文件如下:

块数= 1 板:磷酸盐噪声测定2000x 1.3 PlateFormat终点吸光度原始FALSE 1 1 650 1 12 96 1 8
温度(°C)1 2 3 4 5 6 7 8 9 10 11 12
21.4 0.4977 0.5074 0.5183 0.5128 0.5021 0.5114 0.4993 0.5308 0.4837 0.5286 0.5231 0.5227
0.488 0.4742 0.5011 0.4868 0.4976 0.4845 0.4848 0.5179 0.4772 0.5363 0.5109 0.5197
0.4882 0.4913 0.4941 0.5188 0.4766 0.4914 0.495 0.5172 0.4826 0.5039 0.504 0.5451
0.4771 0.4875 0.523 0.4851 0.4757 0.4767 0.4918 0.5212 0.4742 0.5153 0.5027 0.5235
0.4474 0.4841 0.5193 0.4755 0.4649 0.4883 0.5165 0.5223 0.4799 0.5269 0.5091 0.5191
0.4721 0.4794 0.501 0.4467 0.4785 0.4792 0.4894 0.511 0.4778 0.5223 0.4888 0.5273
0.4122 0.4454 0.314 0.2747 0.4621 0.4416 0.3716 0.2534 0.4497 0.5778 0.2319 0.1038
0.4479 0.5368 0.3046 0.3115 0.4745 0.5116 0.3689 0.3915 0.4803 0.5209 0.1981 0.1062

BLOCKS= 1 Plate: Phosphate Noisiness Assay 2000x 1.3 PlateFormat Endpoint Absorbance Raw FALSE 1 1 650 1 12 96 1 8
Temperature(¡C) 1 2 3 4 5 6 7 8 9 10 11 12
21.4 0.4977 0.5074 0.5183 0.5128 0.5021 0.5114 0.4993 0.5308 0.4837 0.5286 0.5231 0.5227
0.488 0.4742 0.5011 0.4868 0.4976 0.4845 0.4848 0.5179 0.4772 0.5363 0.5109 0.5197
0.4882 0.4913 0.4941 0.5188 0.4766 0.4914 0.495 0.5172 0.4826 0.5039 0.504 0.5451
0.4771 0.4875 0.523 0.4851 0.4757 0.4767 0.4918 0.5212 0.4742 0.5153 0.5027 0.5235
0.4474 0.4841 0.5193 0.4755 0.4649 0.4883 0.5165 0.5223 0.4799 0.5269 0.5091 0.5191
0.4721 0.4794 0.501 0.4467 0.4785 0.4792 0.4894 0.511 0.4778 0.5223 0.4888 0.5273
0.4122 0.4454 0.314 0.2747 0.4621 0.4416 0.3716 0.2534 0.4497 0.5778 0.2319 0.1038
0.4479 0.5368 0.3046 0.3115 0.4745 0.5116 0.3689 0.3915 0.4803 0.5209 0.1981 0.1062

〜结束 原始文件名:2013-08-06磷酸盐噪声;上次保存日期:2013年8月6日7:00:55 PM

~End Original Filename: 2013-08-06 Phosphate Noisiness; Date Last Saved: 8/6/2013 7:00:55 PM

更新 我使用了以下代码:

Update I used this code:

f= open("/Users/Jennifer/Desktop/test.txt", "r")
file_list = f.readlines()

first_twelve = file_list[3:11]

data = [x.replace('\t',' ') for x in first_twelve]
data = [x.replace('\x00','') for x in data]
data = [x.replace(' \r\n','') for x in data]

print data

要获得以下结果: ['21.4 0.4977 0.5074 0.5183 0.5128 0.5021 0.5114 0.4993 0.5308 0.4837 0.5286 0.5231 0.5227','0.488 0.4742 0.5011 0.4868 0.4976 0.4845 0.4848 0.5179 0.4772 0.5363 0.5109 0.5197','0.4882 0.4913 0.4941 0.5188 0.4766 0.4914 0.495 0.5172 0.4826 0.5039 0.504 0.5451','0.4771 0.4875 0.523 0.4851 0.4757 0.4767 0.4918 0.5212 0.4742 0.5153 0.5027 0.5235','0.4474 0.4841 0.5193 0.4755 0.4649 0.4883 0.5165 0.5223 0.4799 0.5269 0.5091 0.5191','0.4721 0.4794 0.501 0.4467 0.4785 0.4792 0.4894 0.511 0.4778 0.5223 0.4888 0.5273','0.4122 0.4454 0.314 0.2747 0.4621 0.3416 0.2534 0.4497 0.5778 0.2319 0.1038','0.4479 0.5368 0.3046 0.3115 0.4745 0.5116 0.3689 0.3915 0.4803 0.5209 0.1981 0.1062']

to get this result: [' 21.4 0.4977 0.5074 0.5183 0.5128 0.5021 0.5114 0.4993 0.5308 0.4837 0.5286 0.5231 0.5227 ', ' 0.488 0.4742 0.5011 0.4868 0.4976 0.4845 0.4848 0.5179 0.4772 0.5363 0.5109 0.5197 ', ' 0.4882 0.4913 0.4941 0.5188 0.4766 0.4914 0.495 0.5172 0.4826 0.5039 0.504 0.5451 ', ' 0.4771 0.4875 0.523 0.4851 0.4757 0.4767 0.4918 0.5212 0.4742 0.5153 0.5027 0.5235 ', ' 0.4474 0.4841 0.5193 0.4755 0.4649 0.4883 0.5165 0.5223 0.4799 0.5269 0.5091 0.5191 ', ' 0.4721 0.4794 0.501 0.4467 0.4785 0.4792 0.4894 0.511 0.4778 0.5223 0.4888 0.5273 ', ' 0.4122 0.4454 0.314 0.2747 0.4621 0.4416 0.3716 0.2534 0.4497 0.5778 0.2319 0.1038 ', ' 0.4479 0.5368 0.3046 0.3115 0.4745 0.5116 0.3689 0.3915 0.4803 0.5209 0.1981 0.1062 ']

哪一个(如果我错了,请纠正我,对Python来说是很新的!)一个列表列表,我应该可以使用它.非常感谢所有回复的人!!!

Which is (correct me if I'm wrong, very new to Python!) a list of lists, which I should be able to work with. Thank you so much to everyone who responded!!!

推荐答案

编写代码lines = f.readlines()时,将返回一行代码.然后,当您说lines[3]时,您将获得第三行.这就是为什么您要使用单个字符的原因.

When you write the code lines = f.readlines() a list of lines is being return to you. When you then say lines[3], you're getting the 3rd line. Thats why you're ending up with individual characters.

您所要做的就是说

files = open("Your File.txt")

file_list =  files.readlines()

first_twelve = file_list[0:12] #returns a list with the first 12 lines

有了first_twelve数组后,就可以使用它进行任何操作.

Once you've got the first_twelve array you can do whatever you want with it.

要打印每一行,请执行以下操作:

To print each line you would do:

for each_line in first_twelve:
    print each_line

这应该对您有用.

这篇关于将文本文件解析为python中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆