How to print next line in python - python

I am trying to print next 3 lines after a match
for example input is :
Testing
Result
test1 : 12345
test2 : 23453
test3 : 2345454
so i am trying to search "Result" string in file and print next 3 lines from it:
Output will be :-
test1 : 12345
test2 : 23453
test3 : 2345454
my code is :
with open(filename, 'r+') as f:
for line in f:
print line
if "Benchmark Results" in f:
print f
print next(f)
its only giving me the output :
testing
how do i get my desired output, help please

First you need to check that the text is in the line (not in the fileobj f), and you can utilise islice to take the next 3 lines from f and print them, eg:
from itertools import islice
with open(filename) as f:
for line in f:
if 'Result' in line:
print(''.join(islice(f, 3)))
The loop will continue from the line after the three printed. If you don't want that - put a break inside the if.

I would suggest opening the file and spliting its content in lines, assigning the outcome to a variable so you can manipulate the data more comfortably:
file = open("test.txt").read().splitlines()
Then you can just check which line contains the string "Result", and print the three following lines:
for index, line in enumerate(file):
if "Result" in line:
print(file[index+1:index+4])

You are testing (and printing) "f" instead of "line". Be careful about that. 'f' is the file pointer, line has your data.
with open(filename, 'r+') as f:
line = f.readline()
while(line):
if "Benchmark Results" in line:
# Current line matches, print next 3 lines
print(f.readline(),end="")
print(f.readline(),end="")
print(f.readline(),end="")
line = f.readline()

It is waiting for the first "Result" in the file and then prints the rest of the input:
import re, sys
bool = False
with open("input.txt", 'r+') as f:
for line in f:
if bool == True:
sys.stdout.write(line)
if re.search("Result",line): #if it should match whole line, than it is also possible if "Result\n" == line:
bool = True
If you want end after first 3 prints, you may add variable cnt = 0 and change this part of code (for example this way):
if bool == True:
sys.stdout.write(line)
cnt = cnt+1
if cnt == 3:
break

with open('data', 'r') as f:
lines = [ line.strip() for line in f]
# get "Result" index
ind = lines.index("Result")
# get slice, add 4 since upper bound is non inclusive
li = lines[ind:ind+4]
print(li)
['Result', 'test1 : 12345', 'test2 : 23453', 'test3 : 2345454']
or as exercise with regex:
import re
with open('data', 'r') as f:
text = f.read()
# regex assumes that data as shown, ie, no blank lines between 'Result'
# and the last needed line.
mo = re.search(r'Result(.*?\n){4}', text, re.MULTILINE|re.DOTALL)
print(mo.group(0))
Result
test1 : 12345
test2 : 23453
test3 : 2345454

Related

How to convert multicharacter single line into string in Python

Hello I have line like below in a file
I want to convert Text :0 to 8978 as a single string. And same for other part i.e Text:1 to 8978.
Text:0
6786993cc89 70hgsksgoop 869368
7897909086h fhsi799hjdkdh 099h
Gsjdh768hhsj dg9978hhjh98 8978
Text:1
8786993cc89 70hgsksgoop 869368
7897909086h fhsi799hjdkdh 099h
Gsjdh768hhsj dg9978hhjh98 8978
I am getting output as
6
7
G
8
7
G
But i want output as from string one and from string two as
6
8
Code is :
file = open ('tem.txt','r')
lines = file.readlines()
print(lines)
for line in lines:
line=line.strip()
linex=line.replace(' ','')
print(linex)
print (linex[0])
I'm not sure about what exact do you need, so:
#1. If need only print the first number (6), I think your code is right.
#2. If you need to print the first part of string(before "space"), it can help you:
line="6786993cc8970hgsksgoop869368 7897909086hfhsi799hjdkdh099h Gsjdh768hhsjdg9978hhjh988978"
print(line[0])
print(line.split(' ')[0])
EDIT
To read a file....
file = open('file.txt', 'r')
Lines = file.readlines()
file.close()
for line in Lines:
print(line.split(' ')[0])
New EDIT
First you need to format your file to after that get the first element. Try this please:
file = open ('tem.txt','r')
lines = file.readlines()
file.close()
linesArray = []
lineTemp = ""
for line in lines:
if 'Text' in line:
if lineTemp:
linesArray.append(lineTemp)
lineTemp = ""
else:
lineTemp += line.strip()
linesArray.append(lineTemp)
for newline in linesArray:
print(newline.split(' ')[0][0])
This should work only if you want to view the first character. Essentially, this code will read your text file, convert multiple lines in the text file to one single string and print out the required first character.
with open(r'tem.txt', 'r') as f:
data = f.readlines()
line = ''.join(data)
print(line[0])
EDITED RESPONSE
Try using regex. Hope this helps.
import re
pattern = re.compile(r'(Text:[0-9]+\s)+')
with open(r'tem.txt', 'r') as f:
data = f.readlines()
data = [i for i in data if len(i.strip())>0]
line = ' '.join([i.strip() for i in data if len(i)>0]).strip()
occurences = re.findall(pattern, line)
for i in occurences:
match_i = re.search(i, line)
start = match_i.end()
print(line[start])

print last line from the variable output in python

I am looking to get the last line produced from the line variable
bash-4.1$ cat file1_dup.py
#!/usr/bin/python
with open("file1.txt") as f:
lines = f.readlines()
for line in lines:
if "!" in line:
line = line.split()[-1].strip()
print line
output i am getting is as follows ..
-122.1058
-123.1050
-125.10584323
The result i wanted to be printed out is
-125.10584323
Moreover, i got the hint from some goghling and getting the output
desired but that seems bit complicated to me at the point ..
bash-4.1$ cat file2_dup.py
#!/usr/bin/python
def file_read_from_tail(fname,n):
with open(fname) as f:
f=f.read().splitlines()
lines=[x for x in f]
for i in range(len(lines)-n,len(lines)):
line = lines[i].split()[-1]
#line = line.split()[-1]
print line
file_read_from_tail('file1.txt',1)
this yeilds teh desired as folows..
bash-4.1$ ./file2_dup.py
-125.10584323
PS: i just borrow the question for the sake of intrest from:
how to read a specific line and print a specific position in this line using python
You could test if the new line is smaller as the one before like this
#!/usr/bin/python
res_line = 0
with open("file1.txt") as f:
lines = f.readlines()
for line in lines:
if "!" in line:
line = float(line.split()[-1].strip())
if res_line > line:
res_line = line
print res_line
Edit:
you can use enumerate() to get the lines indexed in a loop:
with open("file1.txt", "rt") as f:
lines = f.readlines()
for line, content in enumerate(lines):
# apply your logic to line and/or content here
# by adding ifs to select the lines you want...
print line, content.strip() # do your thing
will output (just to illustrate because I didn't specify any conditions in code above):
0 -122.1058
1 -123.1050
2 -125.10584323
3
or in alternative select your specific line with a condition in a listcomp
by using this code instead:
with open("file1.txt", "rt") as f:
lines = f.readlines()
result = [ content.strip() for line, content in enumerate(lines)
if line == len(lines) - 2] # creates a list with
# only the last line
print result[0]
that will output:
-125.10584323
Try the following:
print [line.split()[-1].strip() for line in lines if '!' in line][-1]
I see a better way by creating an empty list and appending the value comes from the condition and then choose the Index of your choice and list the output , this is good in the sense that it can be used for any line of your intrest which you require to pick.
Lets Suppose i wana the last second line then it can be resuable putting the value in the print section print(lst[-2]) , which will print the last index of second line..
#!/usr/bin/python
file = open('file1.txt', 'r')
lst = list()
for line in file:
if "!" in line:
x= line.split()
lst.append(x[-1])
print(lst[-1])

Finding the last element (digit) on every line in a file (Python)

I'm reading from a text file and I need to find the last element (digit) on each line. I don't understand why this code isn't working as I have tried it on a regular string but it doesn't seem to apply in this case.
f = open("file.txt", "r")
result = 0
for line in f:
string = str(f.read())
if string[-1:].isdigit() == True:
result = int(string[-1:])
else:
result = 40
print(result)
f.close()
The file file.txt only contains the line
81 First line32
so the code should print out 2 as a result, but I only get 40, as the first condition never becomes true. What am I doing wrong?
This line is extraneous:
string = str(f.read())
You don't need to read from your file, and will actually move the file pointer by doing so, causing all sorts of issues. You're already reading with this:
for line in f:
Thus, what you want is:
for line in f:
if line[-1:].isdigit() == True:
result = int(line[-1:])
else:
result = 40
This is explained in the documentation.
You have an f.read() too many. This is all you need:
f = open("file.txt", "r")
result = 0
for line in f:
if line[-1:].isdigit():
result = int(line[-1:])
else:
result = 40
print(result)
f.close()
Also the if string[-1:].isdigit() == True: can be replaced with if line[-1:].isdigit():
You may also want to use line.strip() to get rid of new lines, or else the comparison will fail.
f = open("file.txt", "r")
result = 0
for line in f:
l = line.strip()
if l[-1:].isdigit():
result = int(l[-1:])
else:
result = 40
print(result)
f.close()
The problem is that the last character in the line is the end-of-line character. Use .strip() to remove it (it will also remove extra spaces).
with open("file.txt", "r") as f:
for line in f:
lastchar = line.strip()[-1]
if lastchar.isdigit():
result = int(lastchar)
else:
result = 40
print(result)
This prints 2 as you requested in your question with the one-line file.
81 First line32
It will also work for multiple lines, printing the result for each line.

How can I split a text file into multiple text files using python?

I have a text file that contains the following contents. I want to split this file into multiple files (1.txt, 2.txt, 3.txt...). Each a new output file will be as the following. The code I tried doesn't split the input file properly. How can I split the input file into multiple files?
My code:
#!/usr/bin/python
with open("input.txt", "r") as f:
a1=[]
a2=[]
a3=[]
for line in f:
if not line.strip() or line.startswith('A') or line.startswith('$$'): continue
row = line.split()
a1.append(str(row[0]))
a2.append(float(row[1]))
a3.append(float(row[2]))
f = open('1.txt','a')
f = open('2.txt','a')
f = open('3.txt','a')
f.write(str(a1))
f.close()
Input file:
A
x
k
..
$$
A
z
m
..
$$
A
B
l
..
$$
Desired output 1.txt
A
x
k
..
$$
Desired output 2.txt
A
z
m
..
$$
Desired output 3.txt
A
B
l
..
$$
Read your input file and write to an output each time you find a "$$" and increase the counter of output files, code :
with open("input.txt", "r") as f:
buff = []
i = 1
for line in f:
if line.strip(): #skips the empty lines
buff.append(line)
if line.strip() == "$$":
output = open('%d.txt' % i,'w')
output.write(''.join(buff))
output.close()
i+=1
buff = [] #buffer reset
EDIT: should be efficient too https://wiki.python.org/moin/PythonSpeed/PerformanceTips#String_Concatenation
try re.findall() function:
import re
with open('input.txt', 'r') as f:
data = f.read()
found = re.findall(r'\n*(A.*?\n\$\$)\n*', data, re.M | re.S)
[open(str(i)+'.txt', 'w').write(found[i-1]) for i in range(1, len(found)+1)]
Minimalistic approach for the first 3 occurrences:
import re
found = re.findall(r'\n*(A.*?\n\$\$)\n*', open('input.txt', 'r').read(), re.M | re.S)
[open(str(found.index(f)+1)+'.txt', 'w').write(f) for f in found[:3]]
Some explanations:
found = re.findall(r'\n*(A.*?\n\$\$)\n*', data, re.M | re.S)
will find all occurrences matching the specified RegEx and will put them into the list, called found
[open(str(found.index(f)+1)+'.txt', 'w').write(f) for f in found]
iterate (using list comprehensions) through all elements belonging to found list and for each element create text file (which is called like "index of the element + 1.txt") and write that element (occurrence) to that file.
Another version, without RegEx's:
blocks_to_read = 3
blk_begin = 'A'
blk_end = '$$'
with open('35916503.txt', 'r') as f:
fn = 1
data = []
write_block = False
for line in f:
if fn > blocks_to_read:
break
line = line.strip()
if line == blk_begin:
write_block = True
if write_block:
data.append(line)
if line == blk_end:
write_block = False
with open(str(fn) + '.txt', 'w') as fout:
fout.write('\n'.join(data))
data = []
fn += 1
PS i, personally, don't like this version and i would use the one using RegEx
open 1.txt in the beginning for writing. Write each line to the current output file. Additionally, if line.strip() == '$$', close the old file and open a new one for writing.
The blocks are divided by empty lines. Try this:
import sys
lines = [line for line in sys.stdin.readlines()]
i = 1
o = open("1{}.txt".format(i), "w")
for line in lines:
if len(line.strip()) == 0:
o.close()
i = i + 1
o = open("{}.txt".format(i), "w")
else:
o.write(line)
Looks to me that the condition that you should be checking for is a line that contains just the carriage return (\n) character. When you encounter such a line, write the contents of the parsed file so far, close the file, and open another one for writing.
A very easy way would if you want to split it in 2 files for example:
with open("myInputFile.txt",'r') as file:
lines = file.readlines()
with open("OutputFile1.txt",'w') as file:
for line in lines[:int(len(lines)/2)]:
file.write(line)
with open("OutputFile2.txt",'w') as file:
for line in lines[int(len(lines)/2):]:
file.write(line)
making that dynamic would be:
with open("inputFile.txt",'r') as file:
lines = file.readlines()
Batch = 10
end = 0
for i in range(1,Batch + 1):
if i == 1:
start = 0
increase = int(len(lines)/Batch)
end = end + increase
with open("splitText_" + str(i) + ".txt",'w') as file:
for line in lines[start:end]:
file.write(line)
start = end

Python get String behind multiple comma

The problem is i need to read a text.txt file and just get very specific data from it.
the entries of that text.txt looks like this
b(1,4,8,1,4,TEST,0,3,AAAA,Test,2-150,000)
a(1,1,3,1,3,BBBB,0,3,BBBB,Test,2-150,000)
a(1,0,2,1,4,TEST,0,3,CCCC,Test,2-150,000)
b(1,1,0,1,4,TEST,0,3,DDDD,Test,2-150,000)
So now i just whant those lines with "a(" and in those i just need the sting after the 5 and 8 comma, so in line 2 it would be BBBB ,BBBB
my code so far is:
infile = open("text.txt","r")
numlines = 0
found = []
for line in infile:
numlines += 1
if "a" in line:
line=line[line.find("(")+1:line.find(")")]
found.append(line.split(','))
wordLed=len(found)
for i in range(0,wordLed):
print found[i]
infile.close()
This just gives me the full lines seperated at the "," but how can i index though them?
The quick short and dirty:
with open('text.txt') as f:
result = [line.split(',')[5:9:3] for line in f if line.startswith("a(")]
# ^^^^^^^
# "5 to 9 (excl.) by step of 3"
# that is items 5 and 5+3
#
# replace by [5] if you only want the fifths item
# replace by [5:9] if you want items from 5 to 9 (excl.)
from pprint import pprint
pprint(result)
dirty because of the lack of error handling...
... anyway, given your sample data, this produces:
[['BBBB', 'BBBB'], ['TEST', 'CCCC']]
I would use readlines function:
with open("data.txt","r") as f:
lines = f.readlines()
for line in lines:
if line[0:2] == 'a(':
data1 = line.split(',')[5]
data2 = line.split(',')[8]
print(data1, data2)
f.close()
You should check the full condition on start, i.e. a( instead of a. Also you could use split to create an array out of your string, based on ,:
infile = open("text.txt","r")
for line in infile:
if line.startswith("a("): # Starts with a(
data = line.split(',')
print data[5] # Print data at place 5
print data[8] # Print data at place 8
infile.close()
for line in [l for l infile if l.startswith('a(')]
line = line[line.find('('):].strip('()\n').split(',')
a_field, other_field = line[5], line[8]
You split the string already, just index into the list to get the fields you want.

Categories

Resources