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
2 answers

Read the first two lines of each document in a corpus in R

I am having trouble figuring out how to read the first two lines of each document in a corpus in R. The first two lines contain headlines from news articles that I want to analyze. I want to search the headlines (not the rest of each text) for the…
crambone
  • 13
  • 4
1
vote
4 answers

How to print the first n lines of file?

I'm sure I'm missing something obvious and probably asked before but I can't seem to get the right combination of keywords together to give me an answer. How can I write out the first n lines of a file (in effect, the opposite of…
Joe Healey
  • 1,232
  • 3
  • 15
  • 34
1
vote
3 answers

Is readlines() guaranteed to read from the current position rather than the beginning of the file (in all Python implementations)?

Consider: with open('test.txt', 'w') as f: for i in range(5): f.write("Line {}\n".format(i)) with open('test.txt', 'r') as f: f.readline() for line in f.readlines(): print(line.strip()) This outputs Line 1 Line 2 Line…
pingul
  • 3,351
  • 3
  • 25
  • 43
1
vote
2 answers

How to find and replace a line in a text file?

This is my database named myDB.txt: 28273139 iPhone 5.50 30 5 35 81413852 Book 1.50 43 10 55 The text file database follows the format Product Code Product Name Price Current Stock Reorder Stock (the lowest the stock can go) Target Stock Now, the…
vik1245
  • 546
  • 2
  • 9
  • 26
1
vote
1 answer

vb.net to import text file into excel delimited by spaces

was wondering if anyone has an example vb code to import a text file into excel delimited by spaces - regardless of number of spaces. In the text file there are for example 100k lines and in each line, each word can be separated by one, two or three…
Johnseito
  • 315
  • 1
  • 9
  • 24
1
vote
1 answer

Read first N lines using readlines

My python code goes like this with open('file.txt') as w: k = np.asarray(w.readlines(),np.float) But when I do this k is an array with all lines read from file.txt I am trying to figure to read only the first n lines and store k using …
papaya
  • 1,505
  • 1
  • 13
  • 26
1
vote
1 answer

python 3 readline() twice from arduino Serial Monitor

i am working on a function that reads a line from an arduino serial monitor the line outputs: Licht: 870 Temp: 19.01 the first time the function works but after i call the function again it reads an empty line here is my code: import serial import…
littleB0b
  • 13
  • 3
1
vote
2 answers

Python Sorting a file with names and scores

I currently have some data stored in a file that has a persons name and the score that they got when they played the game. The data in the file looks like this: Reece 0 John 5 Alex 2 I have tried using something like this: with…
Reece
  • 59
  • 7
1
vote
1 answer

Plot specific lines for specific values with Pyplot

I'm trying to plot two files of data of this type: name1.fits 0 0 2.40359218172 name2.fits 0 0 2.15961244263 The third column has values from 0 to 5. I want to plot column 2 vs column 4, but, for lines with values in col 3 less than 2 (0 and 1), I…
EternalGenin
  • 495
  • 2
  • 6
  • 14
1
vote
1 answer

How do I apply a function to all items in a list in a text or csv file with Python?

So I've built a function that will look through all the xml files in a folder, and look for a node attribute (speaker name) and write to a row in a csv file. Note, at the moment, it appends them all to the same csv file, but I'm looking to get it to…
1
vote
2 answers

Python - Read from text file and amalgamate information

I have data in a text file that i have no problem reading but need to remove duplicate names and string the values together. See below: boris:1 boris:3 boris:8 tim:4 tim:5 tim:2 ella:3 ella:9 ella:6 I need to remove the duplicate names and add the…
VTX
  • 35
  • 1
  • 4
1
vote
1 answer

'str' object does not support item assignment in python?

I want to read the input.txt line by line and send that as a request to the server and later save the response respectively. how to read and write the data line by line ? my code below works for just one input within input.txt (ex : I am Hungry).…
sam
  • 203
  • 4
  • 17
1
vote
1 answer

Python : Delete certain lines from a file

I'm making a program which deletes certain lines from an existing file. It takes file1 as entry(f1), it looks for a certain pattern and if it finds it, it modifies the line (to make it compatible with the other file) and saves this modification in a…
Luc
  • 9
  • 5
1
vote
2 answers

Finding total number of "stopwords" in a file

I am trying to create a Python program that reads two text files, one containing an article and the other containing a list of "stop words" (one word on each line). I would like to determine how many of these "stop words" are in the specific text…
heyyo9028
  • 19
  • 1
  • 4
1
vote
3 answers

Python Consecutive Reads From File

I have a Python script that is reading from a file. The first command counts the lines. The second one prints the second line although the second one is not working. lv_file = open("filename.txt", "rw+") # count the number of lines…
TheLovelySausage
  • 3,838
  • 15
  • 56
  • 106