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

Python wrong data from file opening

In python digits inside my text file are present in lines. Such as: 1 2 3 When I try to get data from file through readlines() after every digit \n becomes part of that element in list however in such as: ['1\n','2\n' ] However in tutorial it does…
-1
votes
1 answer

How to read a text file partially in Python and join the parts to analyze and plot a histogram efficiently?

I'm facing a problem to read a file with txt format. The file contains a huge amount of data (88604154 lines, 2695.7893953323364 MB) and I have to analyze the data then plot a histogram of them. The problem is that it takes ages for the computer to…
Kia
  • 1
  • 3
-1
votes
1 answer

List index out of range error after trying to read the array from .split() function in python - returns array with only 1 value

I have csv files that I cannot edit from another program. I want ot split the file by the sign ";" using the split function. 1: I open the csv file 2: I use readlines() 3: I use split (;) 4. ERROR when trying to read out splitted value da_split[2]…
-1
votes
1 answer

Python: How to read a text file line by line corresponding to values set by user and ending when line contains certain character

I have a text file that has this format: --------------Run number= 169 ------------ -------Log book info: NaI run--> Test077 Beam ~ 320 nAmp, run duration is 1hr Scalar: 977493, DAQ: 339399 eventno. X Y Z Tb …
theheretic
  • 57
  • 6
-1
votes
1 answer

Closing with "access denied" even with try-catch

I have a C# program that most of the time works fine, but sometimes closes because the file is used by another application. I would like my program to just ignore whenever this happens, so I'm using try-catch whenever I use File.Readlines, but this…
FujiSteen
  • 3
  • 2
-1
votes
2 answers

How to loop through file lines and add them a print list, iterating through

I am trying to put a variable that is in a text file between two strings. For example, I have a text file like this: 123 456 789 I want to loop through each line and append it to two strings: 'The number,' 123 ' appears in the list' 'The number,'…
-1
votes
2 answers

Python Question - How to extract text between {textblock}{/textblock} of a .txt file?

I want to extract the text between {textblock_content} and {/textblock_content}. With this script below, only the 1st line of the introtext.txt file is going to be extracted and written in a newly created text file. I don't know why the script does…
Sauer Lu
  • 11
  • 2
-1
votes
2 answers

File.readlines() doesn't work after running line a second time. Python

For a project, I've been storing data in a text document and I've noticed a problem when trying to receive that data back within the script. More specifically I'm using the readlines() method to get the data. When I use it the first time, it works…
-1
votes
1 answer

read from a file on demand in python

I would like to read a text file word by word when I need it. Like the ifstream in C++. I mean, I want to open the file, then I read the next word from it when I need it, and then close it. How do I do that?
WhoCares
  • 225
  • 1
  • 5
  • 16
-1
votes
1 answer

readlines() python 2.7 vs 3.10

I wrote a script in python 2.7 but want to switch to python 3.10 The only problem is that for some reason the readlines() command isn't producing the same results and is causing problems with my list comp. Below are the two different versions and…
-1
votes
1 answer

Why do I get "str" object has no attribute "readlines" error when running this "read from file" code?

So, I have a file and I need to read in from the columns of the file. The column contains frequency of genes as floats. The number of the column to be read in is column_number variable. Then, all of the floats (there are only floats in the column)…
Maskurate
  • 21
  • 4
-1
votes
2 answers

I don't know how to return and print the function result. (I am making a python address book program)

def search_namecard(): find_IG = input("The IG of the person you are looking for : ") with open('memberinfo_.txt') as f: datafile = f.readlines() for line in datafile: if find_IG in line: return…
이재하
  • 1
  • 5
-1
votes
2 answers

How to find string with readfile if we know only a part of the string?

How can I find a string in a text file if I know only the part of the string? For example, I know only ".someserver.com" But the whole text in the file looks like the following: "hostname1.expedite.someserver.com" So the point is to find the whole…
Oborzil
  • 61
  • 6
-1
votes
1 answer

IndexError: list index out of range in a loop of readlines()

I cant figure out why this gives me an 'IndexError: list index out of range'. I am reading from a simple csv.file and trying to get the values out as separated by commas. with open('project_twitter_data.csv','r') as twf: tw =…
Bluetail
  • 1,093
  • 2
  • 13
  • 27
-1
votes
2 answers

Write a program that first reads in the name of an input file, followed by two strings representing the lower and upper bounds of a search range

I want to write a program that first reads in an input file, followed by two strings representing the lower and upper bounds of a search range. The file should be read using the file.readlines() method. The input file contains a list of…