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
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
2 answers

On the wrong foot with regular python reading of a text file, error line at the exception is wrong

When reading an utf-8 text file in Python you may encounter an illegal utf character. Next you probably will try to find the line (number) containing the illegal character, but probably this will fail. This is illustrated by the code below. Step 1:…
Cornelis
  • 41
  • 2
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
  • 1,205
  • 2
  • 13
  • 21
4
votes
4 answers

How to read an input file of integers separated by a space using readlines in Python 3?

I need to read an input file (input.txt) which contains one line of integers (13 34 14 53 56 76) and then compute the sum of the squares of each number. This is my code: # define main program function def main(): print("\nThis is the last…
Shawn Coward
  • 43
  • 1
  • 4
4
votes
3 answers

Outputting specific lines by number

I want to read lines 25 to 55 from a file, but the range seems to be only outputting a single number and 6 lines, when it should be 30 lines. hamlettext = open('hamlet.txt', 'r') for i in range (25,55): data =…
aiwan
  • 107
  • 7
4
votes
3 answers

codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 318: ordinal not in range(128)

I am trying to open and readlines a .txt file that contains a large amount of text. Below is my code, i dont know how to solve this problem. Any help would be very appreciated. file = input("Please enter a .txt file: ") myfile = open(file) x =…
dtidy
  • 41
  • 1
  • 1
  • 2
4
votes
2 answers

For line in : not returning all lines

I am trying to traverse a text file and take each line and put it into a dictionary. Ex: If the txt file is a b c I am trying to create a dictionary like word_dict = {'a':1, 'b:2', 'c':3} When I use this code: def word_dict(): fin =…
Noob123
  • 528
  • 5
  • 10
4
votes
2 answers

open file and read sentence

I want to open a file and get sentences. The sentences in the file go across lines, like this: "He said, 'I'll pay you five pounds a week if I can have it on my own terms.' I'm a poor woman, sir, and Mr. Warren earns little, and the money meant…
user3119123
  • 85
  • 1
  • 1
  • 6
4
votes
1 answer

Python text file lines keep formatting

I am writing a short script to read in 1000 names from a text file, with each name on its own line. I need to make each name capital, then pad an asterisk onto the beginning and the end. Like * JOHN DOE *. What happens is that when it reads in the…
Sam
  • 55
  • 1
  • 4
4
votes
1 answer

Groovy regex match list from readlines()

I am trying to read a text file and return all lines that do not begin with a #. In python I could easily use list comprehension list with open('file.txt') as f: lines = [l.strip('\n') for l in f.readlines() if not re.search(r"^#", l)] I would…
Chris Powell
  • 175
  • 3
  • 7
3
votes
4 answers

Iterate two lines at a time over text file, while incrementing one line at a time in python

So let's say I have a text file, which contains this: a b c d e I want to iterate through every line of this file, but in the process also get the line following the first line. I have tried this: with open(txt_file, "r") as f: for line1, line2…
hongdekong
  • 41
  • 2
3
votes
6 answers

How to avoid using readlines()?

I need to deal with super large txt input files, and I usually use .readlines() to first read the whole file, and turn it into a list. I know it's really memory-cost and can be quite slow, but I also need to make use of LIST characteristics to…
LookIntoEast
  • 8,048
  • 18
  • 64
  • 92
3
votes
4 answers

readline and readlines methods missing from python 3.2?

Did they remove file.readline() and file.readlines() from python 3.2? If yes what did they replace it with?
nik
  • 8,387
  • 13
  • 36
  • 44
3
votes
2 answers

Skipping last N rows with lapply and then read.csv

I'm an R beginner and trying to read in a number of csv files, remove/skip the last 5 rows from each one, and then rbind them together. I can't figure out which step to do the removal of the rows and what function to use? I've tried readLines below…
1 2
3
33 34