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

Python Function whose arguments specify the lines to retrieve from CSV

Hello there community! I've been struggling with this function for a while and I cannot seem to make it work. I need a function that reads a csv file and takes as arguments: the csv file, and a range of first to last line to be read. I've looked…
margarita
  • 27
  • 3
1
vote
2 answers

Saving a matrix into an array

I want to use the data written in a text file with some headers. The file looks like this: #Fruit list #Prices from different vendors Pineapple 4.49 4.29 5.19 Apple 1.99 1.89 2.19 Banana 1.99 1.99 1.79 Kiwi …
user175924
  • 69
  • 6
1
vote
2 answers

How do I fill in a matrix (by chunks) using a while loop?

I am trying to read in chunks of a a large data set: find the mean of each chunk (representing a larger column) add the mean into a matrix column then find the mean of the means to give me the overall mean of the column. I have the set up, but my…
1
vote
1 answer

How to split a txt into custom paragraphs (and then insert them into excel columns)?

I need to retrieve each section of the text. These sections are identifiable because they start with the words 'First','Second','Third' and so on. Then I need to insert each section in a different column in excel. For example the text reads: First…
1
vote
4 answers

Selecting line from file by using "startswith" and "next" commands

I have a file from which I want to create a list ("timestep") from the numbers which appear after each line "ITEM: TIMESTEP" so: timestep = [253400, 253500, .. etc] Here is the sample of the file I have: ITEM: TIMESTEP 253400 ITEM: NUMBER OF…
kata248
  • 59
  • 4
1
vote
0 answers

Creating a csv file from python readlines()

I want to create a CSV file from a text file text_file.txt Friday,09071235462,08:42:48 Princely,08123456,08:46:45 My code to convert the file #Convert to csv import csv for_csv_list = [] with open(f'./text_file.txt', "r") as file: lines =…
Princely
  • 61
  • 1
  • 8
1
vote
2 answers

Using readline() to read a full line then move onto the next one in .txt file with each subsequent run using Python

I am relatively new to Python, but after reading a lot of posts about using readline() and readlines(), I can't seem to figure this one out. quotes = open("quotes.txt", "r") #Opens the quotes file quote = quotes.readlines() #Uses the readlines()…
Scott Wilks
  • 37
  • 1
  • 5
1
vote
1 answer

Is there any other method I can use to read lines in my code to perform the function of a readLine()?

I am writing a code to count the lines of code with the exception of comments and empty lines but I am stuck at what to use in place of readLine() method with the text variable as it is used only with the BufferedReader class. I do not want to use…
Abubakar Oluyinka
  • 341
  • 1
  • 3
  • 16
1
vote
0 answers

Python bz2 readlines slow in byte-mode

I have a bz2-compressed log file with a lot of lines. Every line has to undergo a small analysis which is of no importance here. I started by reading the lines in text mode like: import bz2 path = 'content.log.bz2' def method_1(path): with…
Durtal
  • 1,063
  • 3
  • 11
1
vote
1 answer

Readlines causing error after many lines?

I'm working on a NRE task at the moment, with data from wnut17train.conll (https://github.com/leondz/emerging_entities_17). It's basically a collection of tweets where each line is a single word from the tweet with an IOB tag attached (separated by…
1
vote
2 answers

How can I extract certain portions of all lines in a text file?

I have a text file that contains several lines of data but I only need to know a little portion of a line. From the initial file, I can narrow it down to the line that contains the information I need but I am unsure how to extract only the necessary…
1
vote
1 answer

Printing contents of a file with and without using readlines() method

I have started learning python and came across the following code to print the contents of a file : with open('Example2.txt','r') as readfile: for line in readfile: print(line) The output was as follows : This is line A This is line…
1
vote
1 answer

why getting lis index out of range error while reading file with readlines()?

I'm only a python beginner so I wanted to practice reading and writing files. (I'm using python 3) Decision = input("""Would you like to 'Write' (W)? \nWould you like to 'Read' (R)?""") if Decision == "W": fileInput =…
1
vote
1 answer

Request Status Code 500 when running Python Script

This is what i am suppose to do: List all files in data/feedback folder Scan all the files, and make a nested dictionary with Title, Name, Date & Feedback (All the files are in Title,Name, Date & Feedback format with each in a different line of…
1
vote
0 answers

Read from a word file in python

How can I read from a word (docx) file in python. I can read from a txt file but can not do the same for MS Office word document. Any suggestions?
Sina Cengiz
  • 136
  • 1
  • 8