readlines

Python (numpy) read a text file with mixed format

▼魔方 西西 提交于 2019-12-07 14:21:27
问题 I have thousands of files like this, and I want to extract the values of columns 6,7,8 for the rows corresponding to atoms ['CG', 'CD1', 'CD2', 'CE1', 'CE2', 'CZ'], ATOM 1 CG TOLU 1 -0.437 -0.756 1.802 1.00 1.99 PRO0 ATOM 2 HG TOLU 1 -0.689 -1.123 2.786 1.00 0.00 PRO0 ATOM 3 CD1 TOLU 1 0.041 -1.623 0.811 1.00 1.99 PRO0 ATOM 4 HD1 TOLU 1 0.331 -2.603 1.162 1.00 0.00 PRO0 ATOM 5 CD2 TOLU 1 -0.692 0.547 1.352 1.00 1.99 PRO0 ATOM 6 HD2 TOLU 1 -1.131 1.264 2.030 1.00 0.00 PRO0 ATOM 7 CE1 TOLU 1 0

Is there a difference between : “file.readlines()”, “list(file)” and “file.read().splitlines(True)”?

烂漫一生 提交于 2019-12-06 22:43:36
问题 What is the difference between : with open("file.txt", "r") as f: data = list(f) Or : with open("file.txt", "r") as f: data = f.read().splitlines(True) Or : with open("file.txt", "r") as f: data = f.readlines() They seem to produce the exact same output. Is one better (or more pythonic) than the other ? 回答1: Explicit is better than implicit, so I prefer: with open("file.txt", "r") as f: data = f.readlines() But, when it is possible, the most pythonic is to use the file iterator directly,

Importing a text file gives error

好久不见. 提交于 2019-12-06 09:14:23
I have a text file that has the following data: 5298 10036 4 360 8 6128 11947 2 385 7 9472 18930 0 233 4 5056 9790 1 293 6 I read this file using the following code: file1 = open("test.txt","r") lines = file1.readlines() BF=[map(float, line.split()) for line in lines] This gives me the following error: could not convert string to float: ÿþ5 Why do I see this error? Update: print lines shows: ['\xff\xfe5\x002\x009\x008\x00\t\x001\x000\x000\x003\x006\x00\t\x004\x00\t\x003\x006\x000\x00\t\x008\x00\r\x00\n', '\x006\x001\x002\x008\x00\t\x001\x001\x009\x004\x007\x00\t\x002\x00\t\x003\x008\x005\x00\t

What substitutes xreadlines() in Python 3?

孤者浪人 提交于 2019-12-05 18:30:13
问题 In Python 2, file objects had an xreadlines() method which returned an iterator that would read the file one line at a time. In Python 3, the xreadlines() method no longer exists, and realines() still returns a list (not an iterator). Does Python 3 has something similar to xreadlines()? I know I can do for line in f: instead of for line in f.xreadlines(): But I would also like to use xreadlines() without a for loop: print(f.xreadlines()[7]) #read lines 0 to 7 and prints line 7 回答1: The file

ReadLines

无人久伴 提交于 2019-12-05 17:48:31
foreach (var item in ReadLines("E:\\bigFile.txt")) { Console.Write(item); } /// <summary> /// 异步读取每行内容 /// </summary> /// <param name="fileName">指定文件的绝对路径</param> /// <returns></returns> public static IEnumerable<string> ReadLines(string fileName) { return ReadLines(delegate { return File.OpenText(fileName); }); } private static IEnumerable<string> ReadLines(Func<TextReader> provider) { using (TextReader reader = provider()) { string line; while ((line = reader.ReadLine()) != null) { yield return line; } } } 来源: https://www.cnblogs.com/wesson2019-blog/p/11938313.html

Python编程小结

本小妞迷上赌 提交于 2019-12-05 07:05:58
为了方便以后查找,记录下平时用到的一些常用方法: 功能:比较两个TXT文件每行的指定位置 import os import time def compute_ap(input,output): with open(input,'r') as f_in: len_f = len(f_in.readlines()) print("compute_ap len_f:",len_f) with open(input,'r+') as f_in: line_1 = f_in.readlines() with open(output,'r+') as f_out: line_2 = f_out.readlines() for row in range(len_f-1): line_1_data = line_1[row].strip('\r\n').split(',') line_2_data = line_1[row].strip('\r\n').split(',') if(abs(float(line_1_data[-2])-float(line_2_data[-1]))): count_temp =count_temp + 1 f_in.close() f_out.close() 功能:读取txt文件,每次一条放入指定txt文件,并执行推理 import os,time import

Using readlines in python? First time

余生颓废 提交于 2019-12-05 03:30:08
I have a text file with columns of data and I need to turn these columns into individual lists or arrays. This is what I have so far f = open('data.txt', 'r') temp = [] for row in f.readlines(): Data = row.split() temp.append(float(Data[0])) When I run this I get IndexError: list index out of range . Snippet of the data below: 16 0.2000 17 0.3000 18 0.4000 20 0.5000 21 0.6000 22 0.7000 24 0.8000 25 0.9000 26 1.000 I need the first column, if possible to look like this: Data = [16, 17, 18, 20, 21, 22, 24, 25, 26] You are getting an empty list Data=[] if you read an empty row. You try to get the

Which function should I use to read unstructured text file into R? [closed]

二次信任 提交于 2019-12-05 02:01:14
This is my first ever question here and I'm new to R, trying to figure out my first step in how to do data processing, please keep it easy : ) I'm wondering what would be the best function and a useful data structure in R to load unstructured text data for further processing. For example, let's say I have a book stored as a text file, with no new line characters in it. Is it a good idea to use read.delim() and store the data in a list? Or is a character vector better, and how would I define it? Thank you in advance. PN P.S. If I use "." as my delimeter, it would treat things like "Mr." as a

What substitutes xreadlines() in Python 3?

不想你离开。 提交于 2019-12-04 02:49:01
In Python 2, file objects had an xreadlines() method which returned an iterator that would read the file one line at a time. In Python 3, the xreadlines() method no longer exists, and realines() still returns a list (not an iterator). Does Python 3 has something similar to xreadlines()? I know I can do for line in f: instead of for line in f.xreadlines(): But I would also like to use xreadlines() without a for loop: print(f.xreadlines()[7]) #read lines 0 to 7 and prints line 7 The file object itself is already an iterable. >>> f = open('1.txt') >>> f <_io.TextIOWrapper name='1.txt' encoding=

Python 文件readlines()方法

ⅰ亾dé卋堺 提交于 2019-12-04 01:02:02
原文连接:https://www.runoob.com/python/file-readlines.html readlines()方法用于读取所有行(直到结束符EOF)并返回列表,该列表可以由python的for...in...结构进行处理。 如果碰到结束符EOF则返回空字符串。 实例: 文件runoob.txt的内容如下: 1:www.runoob.com1 2:www.runoob.com2 3:www.runoob.com3 4:www.runoob.com4 5:www.runoob.com5 #打开文件 fo = open("runoob.txt", "r") # 打开文件 print ("文件名为:", fo.name) # 打印文件名 for line in fo.readlines(): # 依次读取每行   line = line.strip() # 去掉每行头尾空白  print ("读取的数据为:%s" %(line)) fo.close() 以上实例输出结果为: 文件名为: runoob.txt 读取的数据为: 1:www.runoob.com1 读取的数据为: 2:www.runoob.com2 读取的数据为: 3:www.runoob.com3 读取的数据为: 4:www.runoob.com4 读取的数据为: 5:www.runoob.com5