Replacing enumerate to output a string - python

I have written a code that reads a text file containing several paragraphs.I have used enumerate but want to replace enumerate() with a simple loop
file=open("file1.txt","r")
text="target"
for i, line in enumerate(file, 1):
if text in line:
print (i, line)

No idea why you would want to do this, however this is an equivalent:
file=open("file1.txt","r")
text="target"
count=0
for line in file:
count += 1
if text in line:
print (count, line)

enumerate can can be replaced easily with a simple generator function:
def enumerate(iterable, start=0):
for item in iterable:
yield start, item
start += 1

Related

How to use enumerate function in for loop starting from a specific value?

I want this code to cycle through the entire dataset and to be performed each time 'CAFE' is in a line. Currently, it only occurs for the first appearance of CAFE. In other words, I want to specify the start of the enumerate function as the line with CAFE in it, but I am unable to put an argument of start = 'CAFE' inside the enumerate function because the string 'CAFE' cannot be interpreted as an integer.
with open('data.txt', 'r') as dataset:
for line in dataset:
if 'CAFE' in line:
for i, lt in enumerate(dataset):
if i > 1 and i < 130 and i %2 == 0:
lt_1 = int(lt, 16)
lt_2 = float(str(lt_1))
print(lt_2)
else:
pass
else:
pass
Well enumerate(iterable[,start=integer]) takes inetger as start not string and It will be helpful if you provide data.txt
enumerate is equivalent to:
def enumerate(sequence, start=0):
n = start
for elem in sequence:
yield n, elem
n += 1
from python docs

Getting length of each line in a list

I have a block of text and I'd like to add a new line character at the end of any line that is fewer than 50 characters.
This is where I'm at
text = open('file.txt','r+')
line_length = []
lines = list(enumerate(text))
for i in lines:
line_length.append(len(i))
print lines
print line_length
I just end up with a large list of the value 2 over and over. I know that the length of each line is not 2.
Edit: Here's the solution I went with
text = open('text.txt','r+')
new = open('new.txt','r+')
new.truncate(0)
l=[]
for i in text.readlines():
if len(i) < 50:
l.append(i+'\n')
else:
l.append(i)
new.write(' '.join(l))
text.close()
new.close()
Well like:
text = open('file.txt','r+')
l=[]
for i in text.readlines():
if len(i)<50:
l.append(i)
else:
l.append(i.rstrip())
No need for enumerate.
Or one-liner ( i recommend this ):
l=[i if len(i)<50 else i.rstrip() for i in text.readlines()]
So your code doesn't work because really of enumerate.
Both cases:
print(l)
Is desired output.
lines is a list of pairs (each with a length of two). You need to check the length of the sublist, not the pair that it's in:
for i, seq in lines:
line_length.append(len(seq))
Although, as you can see, you don't use i, so there's no point in using enumerate.
Assuming you are trying to write to a new file, you will want something like this:
with open("file.txt", "r+") as input_file, open("output.txt", "w") as output_file:
for line in input_file:
if len(line) < 50:
line += '\n'
output_file.write(line)
The lines in your existing file will often have a newline character at the end of them already, so the result will be two newline characters for lines of length under 50. Use rstrip if you need to avoid this.

Counting the number of occurrences of a specific value in a text file

I have text file with 30 columns. and using python I want to count the number of rows based on the value in column 3. in fact how many times "cement" occurs in column 3. also columns do not have name (or header).
count = 0
with open('first_basic.txt') as infile:
for line in infile:
for j in (line.split()[3]):
if j == "cement":
count += 1
thanks
You are checking each character of 3rd column (word) of every line, to check if it equals cement:
'c' == 'cement' => False
'e' == 'cement' => False
etc.
You should replace
for j in (line.split()[2]):
if j == "cement":
count += 1
with
if line.split()[2] == "cement":
count += 1
Full code:
count = 0
with open('first_basic.txt') as infile:
for line in infile:
if line.split()[2] == "cement":
count += 1
print count
Arrays has the start position as 0 not as 1. So if you want to get the third element of ['DUMMY', 'DUMMY', 'CEMENT', 'NOT_CEMENT'] yoy have to take the [2] position. Because the [3] position is 'NOT_CEMENT'.
And the seccond for, is taking letter by letter, not row by row. The row you took in line.
So to fix your code change:
if line.split()[2] == "cement": #Change here for two
count += 1
But you can take a clean solution for that doing somthing like this:
with open('first_basic.txt') as infile:
map(lambda x: x.split()[2], infile).count('cement')
Let's explain the code.
The map() is responsible to do much like the same as for. It will iterates in all elements of an iterable object. And for each element it apply a function.
The function that was used is this:
lambda x: x.split()[2]
This is a functional way to do this:
def function(x):
return x.split()[2]
But why I used lambda? There is a simple answer, I will not call this function anymore. So I don't need to use space in my memory to this function so I used lambda AKA anonymous function in Python.
You can check about map function here and lambda functions here.
I hope that I helped.
Say you define a predicate function for your matches:
def match(line):
return line.split()[2] == 'cement'
You can use this predicate with a filter and calculate the count of matching lines:
with open('first_basic.txt') as infile:
print(len(list(filter(match, infile.readlines()))))
But this requires memory to build the list first. Using a generator may be faster and does not require the memory for the list:
print(sum(1 for line in infile if match(line))

how to modify strings in a list of strings

I would like to take a list of strings which represent individual lines entered in a CLI and put '~$ ' at the beginning so when I display them it is more clear that they are command lines. I tried this
command = # a multiline block of command lines
lines = command.split('\n')
for l in lines:
l = '~$ ' + l
for l in lines: print l
But this modifies the temporary variable l I think without going back and changing the actual value in the list. If I put the print inside of the first loop it prints with the correct values, but if I do it like shown the change isn't made. Thanks in advance for any help.
Use a list comprehension:
lines = ['~$ ' + line for line in command.split('\n')]
If you have to use a for loop, you'd use enumerate() to include an index so you can replace the individual items in the list:
for i, line in enumerate(lines):
lines[i] = '~$ ' + line
The functional way:
list(map(lambda s: '~$ ' + s, command.splitlines()))

Basic Python loop question - problem reading a list from a text file

I'm trying to read a list of items from a text file and format with square brackets and separators like this: ['item1','item2', .... 'last_item'] but I'm having trouble with the beginning and end item for which I always get: ...,'last_item','], so I do not want the last ,' to be there.
In python I've write:
out_list = "['"
for line in open(file_in):
out_list += line #append the item to the list
out_accession_list += "','" #add the separator
out_accession_list += "]" #add the final closed bracket
return out_list
I realize that this is a basic loop question, but I can't think of the best way to do it. Should I use a try final statement, should it be a while loop, or should I count the number of lines first and then use a loop with a range?
Help much appreciated.
Thanks,
John
Read in all your lines and use the string.join() method to join them together.
lines = open(file_in).readlines()
out_list = "['" + "','".join(lines) + "']"
Additionally, join() can take any sequence, so reading the lines isn't necessary. The above code can be simplified as:
out_list = "['" + "','".join(open(file_in)) + "']"
out_list = []
for line in open(file_in):
out_list.append("'" + line + "'")
return "[" + ",".join(out_list) + "]"
You "right strip" for "," the result before adding the last "]".
e.g. use the string.rstrip(",")
Or
result = "['"
for line in open(file_in):
if len(result) > 0:
result += "','"
result += line
result += "']"
return result
def do(filepath):
out = []
for line in open(filepath, 'r'):
out.append("'" + line.strip() + "'")
return out.__str__()
Your desired output format is exactly Python's standard printable representation of a list. So an easy solution is to read the file, create a list with each line as a string element (stripping the end-of-line of each), and call the Python built-in function repr to produce a string representation of the list:
>>> repr([line.rstrip() for line in open(file_in)])
"['item1', 'item2', 'item3']"

Categories