Questions tagged [readlines]

This tag refers to the `readlines` method available on file objects in Python.

The method's schematics are below:

readlines([size:int]) -> list of strings, each a line from the file.

readlines calls the readline method repeatedly and returns a list of the lines so read. The size argument, if supplied, sets an approximate bound on the total number of bytes in the lines returned.

499 questions
10
votes
2 answers

return the second instance of a regex search in a line

i have a file that has a specific line of interest (say, line 12) that looks like this: conform: 244216 (packets) exceed: 267093 (packets) i've written a script to pull the first number via regex and dump the value into a new file: getexceeds =…
captain yossarian
  • 447
  • 3
  • 10
  • 22
10
votes
13 answers

Counting word frequency and making a dictionary from it

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…
user3323103
  • 103
  • 1
  • 1
  • 4
10
votes
1 answer

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

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…
user2942656
  • 117
  • 1
  • 1
  • 6
9
votes
3 answers

Dealing with readLines() function in R

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…
user3521631
  • 93
  • 1
  • 1
  • 4
9
votes
1 answer

Get total number of non-blank lines from text file?

I am using... File.ReadLines(@"file.txt").Count(); ...to find the total number of lines in the file. How can I do this, but ignore all blank lines?
Keavon
  • 6,837
  • 9
  • 51
  • 79
8
votes
7 answers

Using readlines in python? First time

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() …
user1762768
  • 83
  • 1
  • 1
  • 4
7
votes
2 answers

python - increase efficiency of large-file search by readlines(size)

I am new to Python and I am currently using Python 2. I have some source files that each consists of a huge amount of data (approx. 19 million lines). It looks like the following: apple \t N \t apple n&apos garden \t N \t garden b\ta\md…
6
votes
2 answers

Ignore last \n when using readlines with python

I have a file I read from that looks like: 1 value1 2 value2 3 value3 The file may or may not have a trailing \n in the last line. The code I'm using works great, but if there is an trailing \n it fails. Whats the best way to catch this? My…
faker
  • 213
  • 1
  • 6
  • 15
6
votes
1 answer

Delete lines of a text file in R

I have a .xml file that I read with readLines() in R. I would like to know if there is some function that allow me to delete from line 15 to line 18. I would need of a general command, because I have to repeat the function in loop on the same .xml…
CafféSospeso
  • 1,101
  • 3
  • 11
  • 28
6
votes
1 answer

How to avoid buffering in the Python fileinput library

I've seen this question asked here, but the answers given did not work in my case and was marked duplicate. python -u does not work for stdin in Python 3. sys.stdin = sys.stdin.detach() throws a ValueError: underlying buffer has been detached. None…
dpyro
  • 1,559
  • 2
  • 13
  • 19
6
votes
3 answers

Using "readlines()" twice in a row

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?
Bob
  • 10,741
  • 27
  • 89
  • 143
5
votes
1 answer

How to use readLines in R to read all lines between a certain range?

I am trying to split a large JSONL(.gz) file into a number of .csv files. I have been able to use the code below to create a working .csv file, for the first 25.000 entries. I now want to read and parse the 25.001 to the 50.000th line, and have been…
Frank
  • 53
  • 1
  • 4
5
votes
3 answers

How to read the last line in a textbox?

I have a multiline textbox that constantly gets updated. I need to read only the last word/sentence in the textbox. string lastLine = textBox1.ReadLine.Last();
user3002030
  • 149
  • 1
  • 2
  • 11
5
votes
2 answers

How to add line numbers to a text file in functional programming (F#)?

It works with a for loop and mutable variable: let addLnNum filename = use outFile = new StreamWriter(@"out.txt") let mutable count = 1 for line in File.ReadLines(filename) do let newLine = addPre (count.ToString()) line …
jack3694078
  • 993
  • 1
  • 9
  • 20
5
votes
4 answers

How do I extract multiple character strings from one line using R

I would like to extract multiple character strings from one line. suppose I have the following text line (taken with the 'readLines' function form a website): line <-…
1
2
3
33 34