readlines

Why is it that only once i am able to use file_object.readlines() and file_object.read() ? After that it is empty

Why is it that only once i am able to use file_object.readlines() and file_object.read() ? After that it is empty Question: Code: f = open("file.txt") print(f.readlines()) print(f.readlines()) #NOT ABLE TO USE .READLINES TWICE Output: [‘The Gods and the Titans fought over the ruins of Athens.n’, ‘The Earthling sufferend from these battles.n’, “And the French didn’t …

Total answers: 1

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

Python Question – How to extract text between {textblock}{/textblock} of a .txt file? Question: 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 …

Total answers: 2

Read content of txt files into lists to find duplicates

Read content of txt files into lists to find duplicates Question: I’m new to Python. My code should read 2 different .txt files into lists and compare them to find and delete duplicates. Code import os dir = os.listdir T = "Albums" if T not in dir(): os.mkdir("Albums") with open(‘list.txt’,’w+’) as f: linesA = f.readlines() …

Total answers: 2

How to skip the "n" in the output while using the readlines() method?

How to skip the "n" in the output while using the readlines() method? Question: I want to read a notepad file by using the readlines() method. f = open(‘/home/user/Desktop/my_file’, ‘r’) print(f.readlines()) The output is: [‘Hello!n’, ‘Welcome to Barbara restaurant. n’, ‘Here is the menu. n’] As you see the newlines will be mentioned in the …

Total answers: 2

How do I make a multi-line input in python without using a list?

How do I make a multi-line input in python without using a list? Question: I have to write a program without using list(as I still haven’t learnt them) to take 10 names as the input and put them all in lower-case and then capitalize the first letter. I came up with two programs that both …

Total answers: 4

Function failing to update spacing after comma

Function failing to update spacing after comma Question: I have a csv file that has inconsistent spacing after the comma, like this: 534323, 93495443,34234234, 3523423423, 2342342,236555, 6564354344 I have written a function that tries to read in the file and makes the spacing consistent, but it doesn’t appear to update anything. After opening the new …

Total answers: 2

A question about 'readlines' and 'split()'

A question about 'readlines' and 'split()' Question: For a file contains two strings: True and 26. When I use readlines to get those strings: a, b = file.readlines()[1].split() print(a, b) # True 26 the result is two strings: True and 26. However, when I just want to get the string ’26’ and added another line …

Total answers: 1

Counting word frequency and making a dictionary from it

Counting word frequency and making a dictionary from it Question: I want to take every word from a text file, and count the word frequency in a dictionary. Example: ‘this is the textfile, and it is used to take words and count’ d = {‘this’: 1, ‘is’: 2, ‘the’: 1, …} I am not that …

Total answers: 13

Python readlines() usage and efficient practice for reading

Python readlines() usage and efficient practice for reading Question: I have a problem to parse 1000’s of text files(around 3000 lines in each file of ~400KB size ) in a folder. I did read them using readlines, for filename in os.listdir (input_dir) : if filename.endswith(“.gz”): f = gzip.open(file, ‘rb’) else: f = open(file, ‘rb’) file_content …

Total answers: 2