How to read particular text lines with python? [duplicate] - python

This question already has answers here:
Python: Searching for text between lines with keywords
(2 answers)
Closed last month.
I want to read particular lines from the text file. E.g. all the contents between "This contents information"
I have created a script to perform the task, but it's not a good method. Are there any better way to do it?
readText=open("test.txt","r")
wanted_lines = [4,5,6,7]
count = 1
with open('test.txt', 'r') as infile:
for line in infile:
line = line.strip()
if count in wanted_lines:
print(line)
else:
pass
count += 1

You can compare each line to the sentinel, start outputting once it matches, and stop outputting once it matches again:
with open('test.txt') as infile:
for output in False, True:
for line in map(str.rstrip, infile):
if line == 'This contents information':
break
if output:
print(line)
Demo: https://replit.com/#blhsing/TroubledMysteriousMonitors

You could consider reading the entire text file into a string, and then using a regular expression to extract the contents you want:
with open('test.txt', 'r') as file:
data = file.read()
contents = re.search(r'^This contents information\n(.*?)\nThis contents information\b', inp, flags=re.M|re.S).group(1)
print(contents)
This prints:
City:LK
Country:LL
Postcode:123

You can use split, with "This contents information" as the delimiter.
In the example above, the file will be split into 3 sections, of which we only need to grab the second one (index=1). You can then use .strip() to remove unwanted space.
Code:
with open('test.txt', 'r') as infile:
text = infile.read()
required_info = text.split("This contents information")[1].strip()
print(required_info)
Output:
City:LK
Country: LL
Postcode:123

Instead of prewriting the line numbers, just have a conditional statement that checks for the data you want.
readText=open("test.txt","r")
with open('test.txt', 'r') as infile:
for line in infile:
line = line.strip()
if line == "text to look for":
printline = True
elif line == "text to end content":
printline = False
elif printline == True:
print(line)

I think the best method would be to use regex.
import re
text=""
with open('test.txt', 'r') as infile:
text = infile.read()
# Don't forget to replace here with the word you want to search among what you want to find.
# This contents information(.*?)\nThis contents information
# this regex finds everything between these two words
# example: 'test 123asda test' -> test(.*?)test => ' 123asda '
regex = re.compile(r'This contents information(.*?)\nThis contents information', re.DOTALL)
matches = [m.groups()[0] for m in regex.finditer(text)]
for m in matches:
print(f'{m.strip()}')

import re
with open("file.txt","r") as f:
data =f.readlines()
string="".join(data) #join each line into one string
ls=re.split(r"(\n*?)This contents information\n",string) #split the string where the regex we specified.
for i in range(len(ls)): #print the list. Ohoo you got the answer
print(ls[i])

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])

How to delete specific blank line and concat two line in a text file?

I have an existing txt file(test1) like that,
line1
line2
supp-linex
line3
supp-linex
line4
line5
I want to find the line with "supp" and add this line directly behind the previous line like,(others blank line is not change)
line1
line2linex
line3linex
line4
line5
I know less about how to tackling txt file so in this code,
a_file = open("test1.txt", "r")
lines = a_file.readlines()
a_file.close()
new_file = open("test2.txt", "w")
for line in lines:
if "supp" in line:
#del blank and concat line,I dont know how to del and concat in detail
new_file.write(lines)
new_file.close()
Here is a way that does it without a new list
a_file = open("test.txt", "r")
lines = a_file.readlines()
a_file.close()
new_file = open("test2.txt", "w")
for i, line in enumerate(lines):
if "supp" in line:
j = i
while lines[j-1] == "\n":
del(lines[j-1])
j -= 1
lines[j-1] = lines[j-1].strip() + line.strip("supp-")
del(lines[j])
for line in lines:
new_file.write(line)
new_file.close()
You can use a new list to save the result.
with open("test1.txt") as f:
a_file = f.read().splitlines()
b_file = []
for line in a_file:
if line.startswith('supp-'):
# Removes previous empty lines if possible.
while b_file and len(b_file[-1]) == 0:
b_file.pop()
if b_file:
# Concatenate previous line
b_file[-1] += line[5:]
else:
# When there's no previous lines, appends as is
b_file.append(line[5:])
else:
b_file.append(line)
with open('test2.txt', 'w') as f:
f.write('\n'.join(b_file) + '\n')
You could use re (regular expression) module in Python's standard library to find the pattern and replace it via module's sub() function. To help understand how it works, think of the contents of the whole text file as a single long string containing this:
"line1\nline2\n\nsupp-linex\n\nline3\n\nsupp-linex\nline4\nline5\n"
The regular expression pattern shown in the code below matches a line of characters followed by a blank line, then another prefixed with literal string "supp-". The groups of characters from the match group are also assigned the names prev and extra so they can easily be referred to in the replacement text. The substitution process is applied to the whole file with one sub() call, and then the result of that gets written out to the second text file.
Note: There's a really good Regular Expression HOWTO in the onliinr documentation.
import re
with open('test1.txt', 'r') as a_file:
lines = a_file.read()
pattern = r'''(?P<prev>.+)\n\nsupp-(?P<extra>.+)\n'''
replacement = r'''\g<prev>\g<extra>\n'''
with open('test2.txt', 'w') as new_file:
result = re.sub(pattern, replacement, lines)
new_file.write(result)
print('fini')
Here's the contents of test2.txt after running:
line1
line2linex
line3linex
line4
line5

Python - Identify a string and print all that statement from a text file

I've a text file with following text:
The process runs very well||
It starts at 6pm and ends at 7pm||
The user_id is 23456||
This task runs in a daily schedule!!
I'm trying to see extract all the lines that have the string "user_id". Basically I want to extract this:
The user_id is 23456
My current python code only identify if the desired string exists (or not) in the text file:
word = 'user_id'
if word in open('text.txt').read():
print(word)
else:
print("Not found")
How can I print all the sentences with that contains the word?
Thanks!
You'll want to iterate over the lines to find what you want
word = 'user_id'
with open('text.txt', 'r') as fh:
for line in fh:
if word in line:
print(line)
You are not printing the line, only the word that you are trying to match. Note, the with open() is a nicer way to handle opening and closing files and is functionally similar (but not the same) to
fh = open('text.txt', 'r')
# for loop here
fh.close()
Just do a for loop and iterate through every line, checking if the word is in the line.
word = 'user_id'
for line in open('mean_temp.txt'):
if word in line:
print(line)
output:
The user_id is 23456||
Try this.
word = 'user_id'
not_found = True
with open('text.txt', 'r') as infile:
lines = infile.readlines()
for line in lines:
if word in line:
print(line)
if not_found:
print("Not found")
This is exactly what regular expressions are built for:
import re
with open('text.txt','r') as f:
text = f.read()
sentences = re.findall(r'(.*user.*)',text)
if len(sentences) > 0:
for sentence in sentences:
print(sentence)
else:
print('Not found')

Return First Letter of Line in File

I am trying to pull the first letter of every line in a file, then print those letters to a new file. I am working step-by-step so I created the code that would be able to pull the first letter of every line, however, when I added the code to read a specific file it appears that it is not properly iterating over the entire files content. Does anyone know why my for loop is not iterating? Or perhaps, is the issue that it is iterating but not properly adding the letters to 'lines'.
def secret2(m):
infile = open(m, 'r')
text = infile.read()
for line in text:
lines = text[0]
for i in range(len(text)):
if text[i] == '\n':
lines += text[i+1]
print(lines)
return(lines)
m.close()
Output:
>>> secret2('file.txt')
A
'A'
>>>
Proper output would be:
>>> secret2('file.txt')
'ALICE'
>>>
Your code is iterating over the characters instead of lines. You could print the first character from each line with following code:
def secret2(m):
with open(m) as infile:
print(''.join(line[0] for line in infile if line))
You want to consider the each line as a single data. So use readlines() instead of read. So your code should be
def secret2(m):
infile = open(m, 'r')
text = infile.readlines()
for j in (text):
print j[0]
You can use this:
def get_1st_chr(your_file, id_line) :
with open(your_file) as f :
text_splitted = f.read().splitlines()
f.close()
return text_splitted[id_line][0]
Or, if you want all of the first lines character:
def get_1st_chr(your_file, nb_lines) :
with open(your_file) as f :
text_splitted = f.read().splitlines()
f.close()
for i in range(nb_lines) :
print(text_splitted[[i][0])
You could replace 0 with the id of the character you want to print of course.

I need to open and rewrite a line in a file in Python [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Search and replace a line in a file in Python
How do I modify a text file in Python?
I have an input file that I need to rewrite with the different files needed to be modified before running a program. I have tried a variety of the solutions on here but none of them seem to work. I end up just overwriting my file with a blank file
f = open(filename, 'r+')
text = f.read()
text = re.sub('foobar', 'bar', text)
f.seek(0)
f.write(text)
f.truncate()
f.close()
Or with that code for instance the name I am changing is different each time I run the program so I need to replace the entire line not just one keyword
A simple way may be to read the text into a string, then concatenate the string with the text you want to write:
infile = open('hey.txt','r+')
content = infile.read()
text = ['foo','bar']
for item in text:
content +=item #adds 'foo' on first iteration, 'bar' on second
infile.write(content)
infile.close()
or to change a particular key word:
infile = open('hey.txt','r+')
content = infile.read()
table = str.maketrans('foo','bar')
content = content.translate(table) #replaces 'foo' with 'bar'
infile.write(content)
infile.close()
or to change by line, you can use readlines and refer to each line as the index of a list:
infile = open('hey.txt','r+')
content = infile.readlines() #reads line by line and out puts a list of each line
content[1] = 'This is a new line\n' #replaces content of the 2nd line (index 1)
infile.write(content)
infile.close()
Maybe not a particularly elegant way to solve the problem, but it could be wrapped up in a function and the 'text' variable could be a number of data types like a dictionary, list, etc. There are also a number of ways to replace each line in a file, it just depends on what the criteria are for changing the line (are you searching for a character or word in the line? Are you just looking to replace a line based on where it is in the file?)--so those are also some things to consider.
Edit: Added quotes to third code sample
Though ugly this solution ends up working
infile = open('file.txt', 'r+')
content = infile.readlines() #reads line by line and out puts a list of each line
content[1] = "foo \n" #replaces content of the 2nd line (index 1)
infile.close
infile = open('file.txt', 'w') #clears content of file.
infile.close
infile = open('file.txt', 'r+')
for item in content: #rewrites file content from list
infile.write("%s" % item)
infile.close()
Thanks for all the help!!

Categories

Resources