readlines

Python read specific lines of text

眉间皱痕 提交于 2019-12-25 16:09:17
问题 I am having a problem reading specific lines. It's similar to the question answered here: python - Read file from and to specific lines of text The difference, I don't have a fixed end mark. Let me show an example: -------------------------------- \n ***** SOMETHING ***** # i use this as my start \n -------------------------------- \n data of interest data of interest data of interest \n ----------------------- #this will either be dashes, or EOF ***** SOMETHING ***** -----------------------

Python iterating through two files by line at the same time

让人想犯罪 __ 提交于 2019-12-25 05:17:08
问题 I am trying to compare columns in two files to see if the values match, and if there is a match I want to merge/concatenate the data for that row together. My issue is that when reading line by line from the two files separately, I can't get python to iterate through the files together and look for a match. Instead it will iterate properly through one file and iterate over the same line in the second file multiple times... I have had this issue in the past and still not really found a way

How to create fake text file in Python

时光毁灭记忆、已成空白 提交于 2019-12-21 03:42:32
问题 How can I create a fake file object in Python that contains text? I'm trying to write unit tests for a method that takes in a file object and retrieves the text via readlines() then do some text manipulation. Please note I can't create an actual file on the file system. The solution has to be compatible with Python 2.7.3. 回答1: This is exactly what StringIO/cStringIO (renamed to io.StringIO in Python 3) is for. 回答2: Or you could implement it yourself pretty easily especially since all you need

Python restrict newline characters for readlines()

烂漫一生 提交于 2019-12-21 02:49:08
问题 I am trying to split a text which uses a mix of new line characters LF , CRLF and NEL . I need the best method to exclude NEL character out of the scene. Is there an option to instruct readlines() to exlude NEL while splitting lines? I may be able to read() and go for matching only LF and CRLF split points on a loop. Is there any better solution? I open the file with codecs.open() to open utf-8 text file. And while using readlines() , it does split at NEL characters: The file contents are: "u

Exception : 'unicode' object has no attribute 'readlines' [closed]

情到浓时终转凉″ 提交于 2019-12-19 12:03:50
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I passing a csv file path into this function. def validateCSV(filename): with open(filename, 'rb') as file: print type(filename) if not filename.readlines(): print 'empty file' else: reader = csv.reader(file) for row in reader: print row file.close() but when I run this I got an error 'unicode' object has no

Python read specific lines of text between two strings

二次信任 提交于 2019-12-18 04:21:41
问题 I am having trouble getting python to read specific lines. What i'm working on is something like this: lines of data not needed lines of data not needed lines of data not needed -------------------------------------- ***** REPORT 1 ***** -------------------------------------- [key] lines of interest are here [key] lines of interest are here [key] lines of interest are here [key] lines of interest are here [key] lines of interest are here #This can also be the EOF -----------------------------

Deleting a line from a text file

只谈情不闲聊 提交于 2019-12-17 17:13:09
问题 How to you delete a specific line from a text file using readlines() like: f_open = open("textfile.txt", "r") lines = f_open.readlines() How do you use lines to choose a line in textfile.txt and delete it? Sorry if it doesn't make sense. 回答1: Use the fileinput module's inplace functionality. Refer Optional in-place filtering section at fileinput. The example below deletes the first line from a file: import fileinput import sys for line_number, line in enumerate(fileinput.input('myFile',

Python readlines() usage and efficient practice for reading

隐身守侯 提交于 2019-12-17 02:28:22
问题 I have a problem to parse 1000's of text files(around 3000 lines in each file of ~400KB size ) in a folder. I did read them using readlines, for filename in os.listdir (input_dir) : if filename.endswith(".gz"): f = gzip.open(file, 'rb') else: f = open(file, 'rb') file_content = f.readlines() f.close() len_file = len(file_content) while i < len_file: line = file_content[i].split(delimiter) ... my logic ... i += 1 This works completely fine for sample from my inputs (50,100 files) . When I ran

python 文件读写操作

痴心易碎 提交于 2019-12-16 04:40:42
encoding:utf-8 文件读取操作 fp=open(“E:\file.txt”,“r”,encoding=“utf-8” ) data_read=fp.read()#一次性全部读完 fp.seek(0,0)#游标移动到第一行,继续读,否则读取到的是空 data_readlines=fp.readlines() fp.close() print(data_readlines) print(data_read) 练习:统计文件中一行存在test的行数 注:文件读取的时候,行的末尾包含回车换行符号\n 如果文件很大用readlines读取,小文件直接用read读取,read读取的是整个文件内容,readlines结果是list count=0 fp=open(“e:\file.txt”,“r”,encoding=“utf-8”) lines=fp.readlines() for i in lines: if “test” in i: print(i) count+=1 print(count) read() readlines() readline()的区别 read()—当成一个字符串读出 readlines()readlines返回的是列表 readline()一行一行读文件 如果文件很大,用read()内存不够(如运维日志几十G) 用readline来读超大文件 #原则

Python中readlines()函数的hint参数解读

孤街醉人 提交于 2019-12-15 10:39:32
【释疑】 1.嵩天书上第184页对readlines()函数中的hint参数表述不准确。 2.readlines()函数中的hint参数,含义应该是读入第0~hint个字符。注意换行符计算在内。 3.当hint小于等于0时,读取整个文件的内容。否则,读取到第hint个字符所在行的所有内容。 【实例】 若文件tt.txt的内容如下所示: 12345 123 12 123456 请用readlines()函数编写输出文件tt.txt前三行内容的代码。 【分析】 换行符虽不可见,但需要计数。 第一行加上换行符共计6个字符,下标对应0~5 第二行下标接续上一行编写,对应6~9 第三行下标接续上一行编写,对应10~12 第四行下标接续上一行编写,对应13~19 显然输出文件tt.txt前三行内容的代码如下: fo=open("tt.txt","rt") s=fo.readlines(10) #取10~12均输出前三行内容 print(s) fo.close() 输出文件tt.txt前二行内容的代码如下: fo=open("tt.txt","rt") s=fo.readlines(6) #取6~9均输出前二行内容 print(s) fo.close() 其他以此类推。 来源: CSDN 作者: hnjzsyjyj 链接: https://blog.csdn.net/hnjzsyjyj