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

python read lines in functional way

I need to write a program in a functional way. I have a text and I need to calculate a number of unique words, (for example: "word word, word word?" - has 3 different words, punctuation matters. I have a code: import sys import…
L.Bond
  • 27
  • 5
-2
votes
2 answers

Make python read 12 from file as 12 not 1 and 2

Trying to make a program that provides the price to different numbers of stages. In "tripss.txt",third line is the number 12, python interprets it as 1 and 2 instead and gives the price for each rather than the number as a whole, any way to fix it…
Vik
  • 1
  • 1
-2
votes
2 answers

Incorrect output while reading text file in Python

I need to read the first 30 lines of a file. with open(filename) as f: lines = f.readlines(30) print len(lines) 300 am I missing something?
Bob
  • 9,535
  • 20
  • 81
  • 129
-2
votes
2 answers

Python readlines() function writing [' and \n to file

So I have this code here which I am sure is trivial to my question as it is just question about readlines in general: lines = file_in.readlines() self.inputText.insert(1.0, lines,) If I were to read in a text file, it would write…
Senescence
  • 53
  • 1
  • 6
-3
votes
1 answer

Readlines is only reading the first line in my text file

im trying to convert a program that takes the users string input and defines how many lowercase, uppercase, digits, and characters the string has. Into a program that reads a text file and outputs lower/uppercase/digits/and characters. I have most…
Adrian
  • 1
-3
votes
1 answer

Why is the output of readline() blank in a non-empty text file?

I am running python 3.7 in Wing IDE as an administrator. The python file is in the same folder (Documents) as the textdoc file. This is the python code I have: file = open("textdoc.txt", "r") print(file.readlines()) file.close() This is the…
Clock
  • 25
  • 1
-3
votes
1 answer

How to put a file.txt into more than one list in python

I am trying to put my file.txt into a list and then each index into different lists while all the lists remain in the initial list. I have been able to add it into a single list so far. Here's my code: with open("evenements.txt", "r",…
Oxynor
  • 21
  • 5
-4
votes
1 answer

Reverse block of text every x lines in python

Im sure this is a simple readlines solution. I have spent some time trying to solve it without success. I have a block of text that reads: "This is the text 1" 0.0 2.0 2.0 2.0 0.0 0.0 4.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0…
ZachW
  • 3
  • 4
-4
votes
1 answer

how to read a list of tuples in Python

all, I am learning Python, and I am wondering how to read a list of tuples, which stored in a txt file, as in the following case: I have a txt file called scores.txt with the following format: ('name1', 8) ('name2', 2) ('name3', 4) ... Now I want…
daiyue
  • 6,164
  • 16
  • 67
  • 117
-5
votes
1 answer

Python- How do I combine two outputs and merge them so what prints is a single value? python 2.7

So basically I have this code: try: fhanddate = open('AAPLprice.txt') except: print 'Could not open file - closing application' exit() stock_date = [] for line in fhanddate: p = line.strip() q = p.split(',') r = q[0:] …
Fettucini
  • 31
  • 7
-5
votes
1 answer

How to read a file in R where each line is a vector of variable length ('ragged')

I want to read a file that contain many vector for example you can see below 8984 8813 8685 11629 c(8527, 11629) c(8527, 7685, 7822, 11629) c(8527, 7685, 7822, 7137, 7318, 11629) c(8527, 7685, 7822, 7137, 7318, 7063, 7075, 11629) c(8527, 7685,…
1 2 3
28
29