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.

431 questions
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
  • 5,460
  • 7
  • 43
  • 72
9
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
  • 95
  • 1
  • 1
  • 3
8
votes
12 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
  • 83
  • 1
  • 1
  • 3
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
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
  • 285
  • 3
  • 8
  • 19
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,359
  • 2
  • 12
  • 19
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
  • 129
  • 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
  • 883
  • 1
  • 8
  • 18
5
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
  • 645
  • 2
  • 6
  • 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 <-…
5
votes
2 answers

Python read specific lines of text between two strings

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…
user1443368
  • 121
  • 1
  • 3
  • 9
4
votes
3 answers

TypeError: write() argument must be str, not list while writing to file

I wrote one program which help inrementing the digit in the file. Able to copy only first line if I am using writelines and for f.write I am getting f.write(new_line ) if lines[0].strip().endswith(':') else f.write([new_line, *lines]) …
user1464878
4
votes
2 answers

Python: Read .txt file without putting its content in strings

I have created a .txt file which contains training data for a model. The training samples have a certain structure that looks like this: ("sample sentence", {"entities": [ ]}) I have like 600 of those which I need to put in a list in python.…
TheDude
  • 943
  • 9
  • 16
1
2
3
28 29