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
3
votes
5 answers

Need help in understanding the explanation by Microsoft for File.ReadLines and File.ReadAllLines

According to the explanation by Microsoft for The ReadLines and ReadAllLines methods, When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned. When you use ReadAllLines, you must wait for…
Piush
  • 53
  • 1
  • 7
3
votes
3 answers

readlines gives me additional linebreaks python2.6.5

I have problems with the following code: file = open("file.txt", "r") lines = file.readlines() print lines[0] print lines[1] print lines[2] file.close() This code gives me linebreaks between the lines. So the output is something like…
xamiax
  • 291
  • 1
  • 2
  • 8
3
votes
2 answers

Reading a block of text from a txt file in python between two identical strings

self.viewerData = [] tempViewerData = [] tempViewer = [] started = False with open("tittardata.txt", "r") as fp: for i, line in enumerate(fp.readlines()): if line.startswith("=="): started =…
KMan
  • 31
  • 1
  • 2
3
votes
2 answers

Python (numpy) read a text file with mixed format

I have thousands of files like this, and I want to extract the values of columns 6,7,8 for the rows corresponding to atoms ['CG', 'CD1', 'CD2', 'CE1', 'CE2', 'CZ'], ATOM 1 CG TOLU 1 -0.437 -0.756 1.802 1.00 1.99 PRO0 ATOM …
Vahid Mirjalili
  • 6,211
  • 15
  • 57
  • 80
3
votes
1 answer

Read a file with line continuation characters in Python

I'm looking for a nice pythonic way of reading a file, and joining any lines which are logical continuations of the ones above, as indicated by a line continuation character. E.g. Here is a normal line. This line continues over \ two lines. This…
samwise
  • 129
  • 7
3
votes
5 answers

Read Up Until a Point Python

I have a text file full of data that starts with #Name #main then it's followed by lots of numbers and then the file ends with #extra !side So here's a small snippet #Name #main 60258960 33031674 72302403 #extra !side I want to read only the…
user1985351
  • 4,589
  • 6
  • 22
  • 25
3
votes
5 answers

How to read lines from a json file in scrapy

I have a json file storing some user information including id, name and url. The json file looks like this: {"link": "https://www.example.com/user1", "id": 1, "name": "user1"} {"link": "https://www.example.com/user1", "id": 2, "name": "user2"} This…
Olivia
  • 111
  • 2
  • 6
2
votes
3 answers

How do I use only two strings in this program?

My teacher gave me this question: Write a program that does the following: Enter your first name: Peter Enter your last name: Opunga Enter your birth year: 1992 Hi Peter Opunga, you will be 20 by the end of this year. Bonus 1: Proper commenting.…
user1215225
  • 29
  • 1
  • 1
  • 5
2
votes
3 answers

Python readline() not working?

I am doing a small program to help me with learning Python (which I am very new to). I'm using Python 3.2. In the Python shell, when I enter f = open('filename.txt', 'r') f.readlines() it prints everything in the filename.txt. However, when I type…
Mike
  • 69
  • 1
  • 3
  • 9
2
votes
2 answers

.readlines() returns empty list (solved) -> using csv to add data to a specific named row

After writing a function to generate some data, I wanted to add the ability to save it. I initially started out with the following code which I ran with 'save=True': [in] import csv ... (main body of code - all this works fine) if save is True: …
2
votes
1 answer

delete specific lines and insert new line in python

I got a hardship dealing with some python codes. I've browsed many questions which ask similar questions, but literally I couldn't find keys to solve my problem. what I really want to do is to delete whole specific paragraph that has random…
Parine
  • 71
  • 6
2
votes
1 answer

Python3 - urllib.request.urlopen and readlines to utf-8?

Consider this example: import urllib.request # Python3 URL loading filelist_url="https://www.w3.org/TR/PNG/iso_8859-1.txt" filelist_fobj = urllib.request.urlopen(filelist_url) #filelist_fobj_fulltext =…
sdbbs
  • 4,270
  • 5
  • 32
  • 87
2
votes
5 answers

Loading txt file into R and replace some value based on other data frame

I have a large txt file containing specific format structure. My goal is to load text in R using readLines and I want to replace weight value of each record with new value based on my df data frame. I don't want to change the .txt data structure…
DanG
  • 689
  • 1
  • 16
  • 39
2
votes
3 answers

How to delete specific data from single line in CSV FILE and last line?

I have a CSV file that I have successfully removed lines from using the following code: myfiles = glob.glob('myfile.csv') for file in myfiles: lines = open(file).readlines() open(file, 'w').writelines(lines[27:]) Now…
ThomasGXS
  • 73
  • 7
2
votes
1 answer

Reading a txt file line by line with skip function of every second line and the output saved as a dataframe using R

I would be grateful for some help with reading a text file line by line and skipping lines (1,3,5,7). The input file looks like this: >Q5W0Q7|5-5|ength_1092 DMESPVFAFPKALDLETHIEKLFLY >Q6PEW1|2-2|length_402…
student24
  • 252
  • 1
  • 9