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
2
votes
1 answer

Opening all files in a directory for CSV and reading each line Python

Hi I am looking for some help with regards to a problem I am having. I want to search the directory that my data is in (shown below) for only certain file types. Below is my code but it is not quite functioning. The current output is one of two…
Newbie
  • 23
  • 2
  • 9
2
votes
2 answers

Why can't exit() terminate the program immediately while in the readlines loop?

Part of my program is: f = open('test.txt') for line in f.readlines(): print 'test' exit() Why can't I exit the program immediately when first meet the exit? Instead, my program will exit while the loop has finished. EDIT It happened in the…
waitingkuo
  • 89,478
  • 28
  • 112
  • 118
2
votes
2 answers

Python islice is reading the same lines

I have a big log-file (> 1GB) which should be analysed, so I wrote a python-program. I have used islice so I could read the file in chunks (10,000 lines) so my server won't run out of memory. I've looked up some islice solutions on stackoverflow and…
John Brunner
  • 2,842
  • 11
  • 44
  • 85
2
votes
1 answer

remove line break from each element in python

I open a text file, now each element in the text file is separated by a line break. when I use readlines() It keeps this line break in the list, this is after each element so as it looks like this [zebra\n, ant\n,] i was wondering whether there is a…
Charlie Hardy
  • 175
  • 1
  • 3
  • 14
1
vote
0 answers

readLines in R is not reading complete text file

I am having a problem reading text files that were created when exporting the output from Android's Top command line that was running in a console. The script I am using to read the file is pretty straight forward: file_to_read <-…
1
vote
1 answer

How can I use Python's readlines function to format lines from a file in a specific pattern?

The data I have in the test.txt file looks like this: XXTYSVASOOXOSJAY CGTVHIXXHIAICXWHAYX and I'm trying to achieve a pattern like this: XX-X XX-X-X This is what I have so far: import re data = open("test.txt", "r") lines =…
1
vote
1 answer

readLines() cannot open the connection

I am working in RStudio (RStudio 2023.03.0+386 "Cherry Blossom" Release) and trying to readLines() from an http address that I know is correct. The code is as follows: con <- url("http://biostat.jhsph.edu/~jleek/contact.html") htmlCode <-…
1
vote
1 answer

Skip blank lines in dat file with for loop in Python

I'm trying to write a bit of code that reads in a dat file that has a bunch of blank lines and put them into lists to be manipulated later. Ex: 0.92; 0.70 1.53; 1.41; 1.00 1.47; 1.08 ; 0.73; 0.18 1.50; 1.17 ; ; 1.68; I would like to…
1
vote
1 answer

How do I split a list created from a .txt file with elements separated by "/"?

This is my function: def read_file(): textfile = open("Animals.txt", "r", encoding="utf-8") list = textfile.readlines() print(list) textfile.close() This is Animals.txt: Animal / Hibernation / Waking Hours / Feeding Time Bear /…
Karluk
  • 17
  • 1
  • 6
1
vote
6 answers

python beginner - how to read contents of several files into unique lists?

I'd like to read the contents from several files into unique lists that I can call later - ultimately, I want to convert these lists to sets and perform intersections and subtraction on them. This must be an incredibly naive question, but after…
pandaSeq
  • 213
  • 2
  • 6
  • 12
1
vote
5 answers

How do I make a multi-line input in python without using a list?

I have to write a program without using list(as I still haven't learnt them) to take 10 names as the input and put them all in lower-case and then capitalize the first letter. I came up with two programs that both work; However, there are some…
Annahita
  • 47
  • 4
1
vote
2 answers

How can i replace two lines in a row of equals characters and put just one with that characters?

def deletematerial(): print('Deleting of material ') fh_r = open("AQUESTO.txt", "r") name = input('Enter the name of de material to eliminate: ').lower() print("\n") with open("bb.txt", "w") as output: for line in fh_r: …
1
vote
1 answer

Problem with reading lines from txt or with dictionary usage in Python 3.10

For OOP learning reasons, (for now) created a dictionary class with regions and a class where you can move between regions with limited access, so for example from region 3 you are able only to go to regions 2, 4, 6, 8 or from region 10 only to…
Mr. Chris
  • 35
  • 5
1
vote
5 answers

Is .append() time-consuming?

I've been manipulating huge text files these days. Sometimes I need to delete lines. My way of doing is like below: f=open('txt','r').readlines() list=[] for line in f: if blablablabla: list.append(line) I know for large files,…
LookIntoEast
  • 8,048
  • 18
  • 64
  • 92
1
vote
1 answer

Python: Read specific element from a matrix written in a file with string

I would like to read data from a matrix in a file, the problem is the data is float with string, however, I am interested only in reading the float element, I tried with several options including pandas dataframe, however, I could not so I am kindly…
Sci_tech
  • 27
  • 3