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

Insert multiple lines with for-loop in string created with readlines()

I am trying to insert multiple lines in a string which I have created with readlines() in python. I did many hours of debugging on it but I can't figure out what the problem is. I have a number i of servers which is defined previously. 'data' is a…
Cyvher
  • 41
  • 1
  • 8
1
vote
1 answer

Populate combobox from a txt file python 3

This is a practice project to help me learn python so it's all experimental. I've tried a variety of things and now I'm beginning to get lost in the haze (as you can probably see below). How can I populate a TKinter combobox from a simple txt file…
Lazzo
  • 31
  • 1
  • 4
1
vote
0 answers

Python: concatenate string from file with string literal

I'm trying to concatenate a string pulled from a file with a string literal and getting an error. Example: file.txt: string1 string2 string3 program.py: import random f = open("file.txt", "r") alist = f.readlines() f.close() anotherlist =…
BabaSvoloch
  • 301
  • 3
  • 20
1
vote
0 answers

Reading file unicode and converting to english Python

Im currently having some troubles reading a file with Python 3. When I open the file as plain text, I can read the following text: sadfdasfsafasdf,asdfdasfdsafdasfdasf,asdfasfasddfa,asfdasfadd However when I read the file with the following code…
chuseuiti
  • 783
  • 1
  • 9
  • 32
1
vote
1 answer

Deleting the previous line

I don't know is possible deleting previous line what the program saving in memory? In the file "Server.txt" I am looking for word: Server but the file overwrites all the time and memory remember all of this line where line starting with "Server"…
Gracjan
  • 35
  • 5
1
vote
1 answer

How To Read Lines of CSV or Text File, Loop Over Each Line and Save To a New File For Each Line Read

I have a unique problem that I thought I had solved until I hit the wall with using While Loop to control the flow of this program. Synopsis: I have a flat file (CSV or text) that has some URLs that I want to scrape, append a new tag to the HTML…
Chad Buie
  • 13
  • 6
1
vote
1 answer

Using while and if in python to create a game fueled by a text file

I am attempting to ask a question, have the user input the answer. The program has to check the user's answer against the correct answer than keep score. I am having difficulties formatting the while and if statements and reading the lines from…
1
vote
1 answer

Find the first line starting with a special symbol in R

I have a set of text files. In each one the useful information starts from the 1st line beginning with !. In the example shown below, useful information starts from line 3. LINES,1 TYPES,T1,I,I,N,T1 !,CODE,GROUP,MTH,COMP *,1,16,1,"A" However, this…
LaTeXFan
  • 1,136
  • 4
  • 14
  • 36
1
vote
1 answer

Ruby: Reading Lines from File Splitting them into Struct

I have a text file that I want to read which is line separated in this format "Item1", 1 "Item2", 34 "Item3", 6 etc. I want to input each part of the line into a new instance of my Struct. What syntax should I use to accomplish this? I'm hitting a…
Bill Bisco
  • 129
  • 6
1
vote
1 answer

Why is grep() not working after readLines()?

I developed a program in R to read a report available online and the first 2 lines are: page1 <-…
ViniLima
  • 13
  • 3
1
vote
1 answer

Printing links from a text file produces "line gap" in between

When I print links stored in a text file, the result I'm getting constantly kinda weird which is in this case a line gap between each link. If i use .strip() then it fixes the problem but it doesn't seem satisfactory usage. My question is: how can i…
SIM
  • 21,997
  • 5
  • 37
  • 109
1
vote
1 answer

How to write nested list back to text file so that there is one list per line?

Question: How to write a nested list from a variable into a text file so that each list inside the nested list is written into it's own line? The list is needed to be compiled back into the text file in exactly the same format as it was before…
Kasperi Koski
  • 55
  • 1
  • 5
1
vote
2 answers

Python - Using .readlines() with .rstrip() and then store all words into a list

I want to be able to strip the \n character ( .rstrip('\n') ) from a text file (dictionary.txt) that contains 120,000+ words. then counts each line and returns the amount of words in the txt file (each word is on its own line). then finally want…
MLJezus
  • 79
  • 1
  • 9
1
vote
3 answers

Why does readline() not work after readlines()?

In Python, say I have: f = open("file.txt", "r") a = f.readlines() b = f.readline() print a print b print a will show all the lines of the file and print b will show nothing. Similarly vice versa: f = open("file.txt", "r") a =…
Svavinsky
  • 69
  • 3
1
vote
1 answer

Trying to add text from a random line from a text file

I am making a game in pygame for my sister for practising in pygame and I am trying to add a random line from a text file in to window name: import pygame,random,sys RandomMess = open('GameText.txt') pygame.display.set_caption('Dream Land:…
tygzy
  • 698
  • 1
  • 6
  • 23