readlines

Python read specific lines of text between two strings

时光怂恿深爱的人放手 提交于 2019-11-29 05:02:16
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 -------------------------------------- ***** REPORT 2 ***** -------------------------------------- lines of data not needed lines of

sys.stdin.readlines() hangs Python script

江枫思渺然 提交于 2019-11-29 03:51:56
Everytime I'm executing my Python script, it appears to hang on this line: lines = sys.stdin.readlines() What should I do to fix/avoid this? EDIT Here's what I'm doing with lines : lines = sys.stdin.readlines() updates = [line.split() for line in lines] EDIT 2 I'm running this script from a git hook so is there anyway around the EOF? This depends a lot on what you are trying to accomplish. You might be able do: for line in sys.stdin: #do something with line Of course, with this idiom as well as the readlines() method you are using, you need to somehow send the EOF character to your script so

Dealing with readLines() function in R

岁酱吖の 提交于 2019-11-29 02:10:47
I'm experiencing a very hard time with R lately. I'm not an expert user but I'm trying to use R to read a plain text ( .txt ) file and capture each line of it. After that, I want to deal with those lines and make some breaks and changes in the text. Here is the code I'm using: fileName <- "C:/MyFolder/TEXT_TO_BE_PROCESSED.txt" con <- file(fileName,open="r") line <- readLines(con) close(con) It reads the text and the line breaks perfectly. But I don't understand how the created object line works. The object line created with this code has the class: character and the length [57] . If I type

python读文件

岁酱吖の 提交于 2019-11-29 00:39:36
1.open with open(‘1.txt’, ‘r’) as f: print(f.readlines()) read() 每次读取整个文件,它通常用于将文件内容放到一个字符串变量中。如果文件大于可用内存,为了保险起见,可以反复调用read(size)方法,每次最多读取size个字节的内容。 readlines() 一次读取整个文件,readlines() 自动将文件内容分析成一个行的列表,该列表可以由 Python 的 for … in … 结构进行处理。 readline() 每次只读取一行,通常比readlines() 慢得多。仅当没有足够内存可以一次读取整个文件时,才应该使用 readline()。 注意:这三种方法是把每行末尾的’\n’也读进来了,它并不会默认的把’\n’去掉,需要我们手动去掉。 with open(‘test1.txt’, ‘r’) as f1: list1 = f1.readlines() In[3]: list1 Out[3]: [‘111\n’, ‘222\n’, ‘333\n’, ‘444\n’, ‘555\n’, ‘666\n’] 去掉’\n’ with open(‘test1.txt’, ‘r’) as f1: list1 = f1.readlines() for i in range(0, len(list1)): list1[i]

Counting word frequency and making a dictionary from it

三世轮回 提交于 2019-11-28 11:38:11
I want to take every word from a text file, and count the word frequency in a dictionary. Example: 'this is the textfile, and it is used to take words and count' d = {'this': 1, 'is': 2, 'the': 1, ...} I am not that far, but I just can't see how to complete it. My code so far: import sys argv = sys.argv[1] data = open(argv) words = data.read() data.close() wordfreq = {} for i in words: #there should be a counter and somehow it must fill the dict. If you don't want to use collections.Counter, you can write your own function: import sys filename = sys.argv[1] fp = open(filename) data = fp.read()

用Python读取大文件

♀尐吖头ヾ 提交于 2019-11-28 11:02:07
通常我们在读取文件的时候,会用到read(), readline(), readlines()。 通常可能会有这样的用法: [python] view plain copy def test1(): with open( "/tmp/test.log", "r") as f: print f.read() 或者 [python] view plain copy def test2(): f = open( "/tmp/test.log", "r") for line in f.readlines(): print line f.close() read ()的方法是一次性把文件的内容以字符串的方式读到内存, 放到一个字符串变量中 readlines()的方法是一次性读取所有内容, 并按行生成一个list 因为read()和readlines()是一次性把文件加载到内存, 如果文件较大, 甚至比内存的大小还大, 内存就会爆掉。 所以,这两种方法只适合读取小的文件。 实际工作中,会碰到读取10几G的大文件的需求, 比如说日志文件。 这时候就要用的新的读取文件的方法。 这里提供两种方法, 有简单,有复杂,但基本原理都是一样的。 就是利用到生成器generator。 方法一: 将文件切分成小段,每次处理完小段内容后,释放内存 这里会使用yield生成自定义可迭代对象, 即generator

Deleting a line from a text file

泪湿孤枕 提交于 2019-11-28 02:28:03
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. 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', inplace=1)): if line_number == 0: continue else: sys.stdout.write(line) nico You cant delete a line from a file

Using “readlines()” twice in a row

巧了我就是萌 提交于 2019-11-28 02:17:16
I'm trying to do something like this: Lines = file.readlines() # do something Lines = file.readlines() but the second time Lines is empty. Is that normal? Yes, because .readlines() advances the file pointer to the end of the file. Why not just store a copy of the lines in a variable? file_lines = file.readlines() Lines = list(file_lines) # do something that modifies Lines Lines = list(file_lines) It'd be far more efficient than hitting the disk twice. (Note that the list() call is necessary to create a copy of the list so that modifications to Lines won't affect file_lines .) You need to reset

Dealing with readLines() function in R

≯℡__Kan透↙ 提交于 2019-11-27 21:49:41
问题 I'm experiencing a very hard time with R lately. I'm not an expert user but I'm trying to use R to read a plain text ( .txt ) file and capture each line of it. After that, I want to deal with those lines and make some breaks and changes in the text. Here is the code I'm using: fileName <- "C:/MyFolder/TEXT_TO_BE_PROCESSED.txt" con <- file(fileName,open="r") line <- readLines(con) close(con) It reads the text and the line breaks perfectly. But I don't understand how the created object line

Counting word frequency and making a dictionary from it

℡╲_俬逩灬. 提交于 2019-11-27 19:20:14
问题 I want to take every word from a text file, and count the word frequency in a dictionary. Example: 'this is the textfile, and it is used to take words and count' d = {'this': 1, 'is': 2, 'the': 1, ...} I am not that far, but I just can't see how to complete it. My code so far: import sys argv = sys.argv[1] data = open(argv) words = data.read() data.close() wordfreq = {} for i in words: #there should be a counter and somehow it must fill the dict. 回答1: If you don't want to use collections