inputFile = open('original_text.txt','r')
outputFile = open('half_text.txt','w')
line = inputFile.readline()
count = 0
for line in inputFile:
outputFile.write(line)
count += 1
if count % 2 == 0:
print(line)
inputFile.close()
outputFile.close()
It keeps skipping the 1st line. For instance, the text file right now has 10 lines. So it prints the 3rd 5th 7th and 9th. So I'm just missing the first.
This skips the first line because you read it and throw it away before the loop. Delete line 4,
line = inputFile.readline()
Add change the count parity to odd with
if count % 2 == 1:
For a slightly better design, use a boolean that toggles:
count = False
for line in inputFile:
outputFile.write(line)
count = not count
if count:
print(line)
inputFile.close()
outputFile.close()
I tried running the program on itself:
inputFile = open('this_file.py', 'r')
count = False
outputFile.write(line)
if count:
outputFile.close()
use next to skip the next line. You may need to watch for a StopIteration error on the call to next(fh) if you have odd lines.
outputFile = open('half_text.txt','w')
with open('original_text.txt') as fh:
for line1 in fh:
outputFile.write(line1)
try:
next(fh)
except StopIteration:
pass
outputFile.close()
The for loop will go over the file line by line and when you use the readline, it will advance the pointer forward inside the loop. Therefore odd will go over odd numbered lines and even goes over even numbered lines.
with open (path, 'r') as fi:
for odd in fi:
even = fi.readline()
print ('this line is odd! :' + odd)
print ('this line is even! :' + even)
Related
I need to make my python code reads emma.txt file after line 32 to line 16266.
fin = open("emma.txt", encoding="UTF-8")
fin.readline()
line= fin.readline()
word = line.strip()
print(word)
fin.close()
fin = open("emma.txt", encoding="UTF-8")
for line in fin:
word = line.strip()
print(word)
The problem is solved with the code below
emma = open("emma.txt", encoding="UTF-8")
lines = emma.readlines()[31:16265]
for line in lines:
print(line.rstrip())
Since storing all lines into memory is not a good idea. you can use islice from itertools module which gives you ability to pick which items you want from an iterator. click
from itertools import islice
with open('your_file.txt') as f:
for line in islice(f, 31, 16266):
# do what you wanna do
It is much more efficient and clean.
Try this:
with open("test.py", "r") as file:
lines = file.readlines()
lines = lines[31:16265]
print(lines)
This saves all the lines as a list and then makes the list the 31st index (32nd line) to the 16265th index (16266th line).
My mistake, my previous answer was incorrect. I have changed it to
lines = lines[21:12265]
Use this to get your answer fast and efficient
start = 31
end = 16266
with open("test.py", "r", encoding="UTF-8") as file:
lines = file.readlines()[start:end]
print(lines)
def read_range_lines(filename, start, end):
if end > start:
return
with open(filename, "r") as file:
count = 0
while count < start - 1:
file.readline()
count += 1
while count < end:
print(file.readline())
count += 1
Using enumerate to track line numbers.
with open('f3.txt') as f:
for line_no, line in enumerate(f):
if 31 <= line_no <= 16265: # assuming line numbered from 0
# Processing of desired lines
print(line)
elif line_no > 1626i5:
# Not required but avoids futher unnecessary file reading
break
I am using the following code to read a text file and create an array list out of the text found in the file.
But currently I am reading the whole file.
How can I read for example from line no. 16 onwards in this case?
array = []
with open(path, 'r') as f:
for line in f.readlines():
for action in all_actions:
if action in line:
array.append(action)
You can use
array = []
exit_line = 16
start_line = 10
with open(path, 'r') as f:
for index, line in enumerate(f.readlines()[start_line:]):
for action in all_actions:
if action in line:
array.append(action)
if index == exit_line - 1:
break
and then to make an if condition to exit/ break at line 16 that is the index +1 .
Try (explanation in code comments):
array = []
with open('path', 'r') as f:
# x here is a loop (lines) counter provided by the enumerate function
for x, line in enumerate(f.readlines()):
for action in all_actions:
if action in line:
array.append(action)
# if the counter reaches 16 BREAK out of the loop
if x == 16:
print ("Line 16 exiting loop")
break
This only prints the last line that's 2 in length. How can I make it print all of the previous lines 2 in length? This is where I am so far:
file_name = "file1.txt"
with open(file_name, "r") as f:
for line in f:
if len(line) == 2:
print line
You need to use strip() to strip the white space from the beginning and the end of each line to get correct line length. Otherwise your code should work fine.
file_name = "file1.txt"
with open(file_name, "r") as f:
for line in f:
if len(line.strip()) == 2:
print line
The line to be modified is line 5:
if len(line.strip()) == 2:
I have a text document of 7000 words that I have to go through and count the letters in each word. So I am stuck on trying to break up each line at the '/n' and having it count it. If someone could point me in the right direction, it would be very much appreciated.
from string import *
def main():
fname = raw_input("Enter filename: ")
infile = open(fname,'r')
lines = 0
for line in infile.readlines():
lines = lines + 1
letters = line.split(line)
x = str(letters)
print len(x)
print line
print lines
main()
You don't need to break up the file into lines using a linefeed, Python will do that for you. In your comment you said the file had one word per line, so you could do this:
line_count = 0
with open("test.txt") as f:
for line in f:
line_count += 1
print len(line)
print line
print line_count
I am trying to compare two files, A and C, in Python and for some reason the double for loop doesn't seem to work properly:
with open(locationA + filenameC,'r') as fileC, open(locationA + filenameA,'r') as fileA:
for lineC in fileC:
fieldC = lineC.split('#')
for lineA in fileA:
fieldA = lineA.split('#')
print 'UserID Clicks' + fieldC[0]
print 'UserID Activities' + fieldA[0]
if (fieldC[0] == fieldA[0]) and (fieldC[2] == fieldA[2]):
print 'OK'
Here, only the line of C seems to be compared, but for the other lines, the "A loop" seems to be ignored.
Can anyone help me with this?
Your problem is that once you iterate over fileA once you need to change the pointer to the beginning of the file again.
So what you might do is create two lists from both files and iterate over them as many times as you want. For example:
fileC_list = fileC.readlines()
fileA_list = fileA.readlines()
for lineC in fileC_list:
# do something
for lineA in fileA_list:
# do somethins
The problem with nested loops (from the point of view of your current problem) is precisely that the inner loop runs to completion for each iteration of the outer loop. So instead, set lineA by calling for the next item from the fileA iterator explicitly:
with open(locationA + filenameC,'r') as fileC, open(locationA + filenameA,'r') as fileA:
for lineC in fileC:
fieldC = lineC.split('#')
lineA = next(fileA)
fieldA = lineA.split('#')
print 'UserID Clicks' + fieldC[0]
print 'UserID Activities' + fieldA[0]
if (fieldC[0] == fieldA[0]) and (fieldC[2] == fieldA[2]):
print 'OK'
This logic will ignore any extra lines from fileA once fileC is exhausted, and if fileC contains more lines than FileA things might also get ugly without special checks.
A different approach might use itertools.izip() to collect lines from each file in pairs:
import itertools
with open(locationA + filenameC,'r') as fileC, open(locationA + filenameA,'r') as fileA:
for lineC, lineA in itertools.izip(fileC, fileA):
fieldC = lineC.split('#')
fieldA = lineA.split('#')
print 'UserID Clicks' + fieldC[0]
print 'UserID Activities' + fieldA[0]
if (fieldC[0] == fieldA[0]) and (fieldC[2] == fieldA[2]):
print 'OK'
I can't think of any specific reason to use one instead of the other, but if the files are of any size at all refuse the temptation to use the builtin zip() function instead of itertools.izip() - the former returns a list, and so memory usage depends on file sizes, whereas the latter is a generator, and so creates values as they are required.
You are comparing all lines from FileA to each line from FileC. That means, for each line of File C, you will read the entire FileA, and (provided you do move the pointer to the beginning of the File A), you would read it again, and again.
It is easier to read them both at the same time while they both have lines
if they are the same, do something, read from both
if they are different, read from the smallest (Line A < Line C, read from File A only; Line C < Line A, read from Line C only)
and make two last loops while there are remaining lines (two loops, one for each file, as you do not know which one ran out of lines)
I know this is an old thread but it comes up on google when someone is looking for a solution to compare 2 text files in python.
This code worked for me.
You can update the codes and use "with open" instead and fine tune as you like but it does the job.
# Ask the user to enter the names of files to compare
fname1 = input("Enter the first filename (text1.txt): ")
fname2 = input("Enter the second filename (text1.txt): ")
# Open file for reading in text mode (default mode)
f1 = open(fname1)
f2 = open(fname2)
# Print confirmation
print("-----------------------------------")
print("Comparing files ", " > " + fname1, " < " +fname2, sep='\n')
print("-----------------------------------")
# Read the first line from the files
f1_line = f1.readline()
f2_line = f2.readline()
# Initialize counter for line number
line_no = 1
# Loop if either file1 or file2 has not reached EOF
while f1_line != '' or f2_line != '':
# Strip the leading whitespaces
f1_line = f1_line.rstrip()
f2_line = f2_line.rstrip()
# Compare the lines from both file
if f1_line != f2_line:
# If a line does not exist on file2 then mark the output with + sign
if f2_line == '' and f1_line != '':
print(">+", "Line-%d" % line_no, f1_line)
# otherwise output the line on file1 and mark it with > sign
elif f1_line != '':
print(">", "Line-%d" % line_no, f1_line)
# If a line does not exist on file1 then mark the output with + sign
if f1_line == '' and f2_line != '':
print("<+", "Line-%d" % line_no, f2_line)
# otherwise output the line on file2 and mark it with < sign
elif f2_line != '':
print("<", "Line-%d" % line_no, f2_line)
# Print a blank line
print()
#Read the next line from the file
f1_line = f1.readline()
f2_line = f2.readline()
#Increment line counter
line_no += 1
# Close the files
f1.close()
f2.close()