Set list of str and set name of list automaticly - python-3.x

i am encountering an issue with my program, i would like the program to recognize string from a txt using two columns, then to put the strings of the txt in a list with the name of rows in the two columns, i don't know how to create list automatically now, but i still want to make a program that everything he recognize in the txt is put in a list.
link = "excelfilett.txt"
file = open(link, "r")
frames_path = []
is_count_frames_path = False
for line in file:
if "[Routing Paths]" in line:
is_count_frames_path = True
if is_count_frames_path== True:
for row in df_frame_array_load["[Name]"]:
if row in line:
a = line[0:4]
a = list(a)
if "[EthernetComNeed]" in line:
break
for row in df_frame_array_load["[Name2]"]:
if row in line:
a = line[0:4]
a = list(a)
if "[EthernetComNeed]" in line:
break
if "[EthernetComConfig]" in line:
break
print(a)
But it only gives me this:
['F', 'L', '_', '9']
While i want something like:
['FL_1', 'FL_1', 'FL_2', 'FL_9']
Any ideas on how to do this?

Related

Find all files in a directory and process only ones that have a specific line, skip if does not

Need help, I’m new to python,
I wrote a script that should find all files in a directory and process only ones that have a specific line and skip the ones that do not have the line. The specific line is ‘," Run Time’
it fails to process ONLY files I need, it processes all files.
All Lines to find:
‘," Run Time’
‘,” Start Time’
‘,” End Time’
‘Test_ID e:’
‘Test Program Name:’
‘Product:’
Lines 1, 2 and 3 are repeating lines and I need them all,
Lines 4, 5 and 6 also repeating but I need to capture them only ones.
import os
runtime_l = '," Run Time'
start_tm = '," Start Time'
end_tm = '," End Time'
test_ID = ' Host Name: '
program_n = 'Test Program Name:'
prod_n = 'Product:'
given_path = 'C:\\02\\en15\\TST'
for filename in os.listdir(given_path):
filepath = os.path.join(given_path, filename)
if os.path.isfile(filepath):
print("File Name: ", filename)
print("File Name\\Path:", filepath)
with open(filepath) as mfile:
for line in mfile:
if runtime_l in line:
# do something with the line
print(line)
if start_tm in line:
# do something with the line
print(line)
if end_tm in line:
# do something with the line
print(line)
if test_ID in line:
# do something with the line
print (line)
if program_n in line:
# do something with the line
print (line)
if prod_n in line:
# do something with the line
print (line)
else:
continue
Hers is snippet on how I would test a file if it has a "Run Time" line.
Not sure if it is "pythony" looking script but it does what I need. It finds files with the line I want and proccesses them.
import os
runtime_l = '," Run Time'
start_tm = '," Start Time'
end_tm = '," End Time'
given_path = 'C:\\02\\en15\\TST'
for filename in os.listdir(given_path):
filepath = os.path.join(given_path, filename)
if os.path.isfile(filepath):
#print("File Name: ", filename)
#print("File Name\\Path:", filepath)
with open(filepath) as mfile:
for line in mfile:
if runtime_l in line:
#runtime_file = open(filepath, 'r')
with open(filepath) as runtime_file:
for rn_l in runtime_file:
if runtime_l in rn_l:
print (rn_l)
elif start_tm in rn_l:
print (rn_l)
elif end_tm in rn_l:
print (rn_l)
Using a generator with any() to determine if a certain string is in a file:
if any(tgt_str in line for line in my_file):
print("string found")
else:
print("not found")
any() hosts a generator and will stop at the first line that has the target string and return True. If the string isn't in the file, it will be scanned to the end and any() will return False. See below for a very brief intro to generators and list comprehensions.
any(line for line in my_file if tgt_str in line)
Also works. next() can also be used, but it will raise a StopIteration if the string isn't found in the file.
The below preserves the majority of your code with some additions.
runtime_l = '," Run Time'
start_tm = '," Start Time'
end_tm = '," End Time'
test_ID = ' Host Name: '
program_n = 'Test Program Name:'
prod_n = 'Product:'
class OneTime:
def __init__(self):
self.test_ID = 0
self.program_n = 0
self.prod_n = 0
def all_done(self):
return self.test_ID & self.program_n & self.prod_n
given_path = './test_logs'
for filename in os.listdir(given_path):
filepath = os.path.join(given_path, filename)
if not os.path.isfile(filepath):
continue
with open(filepath, 'r') as mfile:
if not any(runtime_l in ln for ln in mfile):
continue # Onto the next file...
# If we get this far, we know the runtime_l string is in
# the file. Proceed...
mfile.seek(0) # Back to start of file.
one_time = OneTime()
one_time_done = 0
print("File Name: ", filename)
print("File Name\\Path:", filepath)
for line in mfile:
line = line.rstrip()
if runtime_l in line:
# do something with the line
print(line)
elif start_tm in line:
# do something with the line
print(line)
elif end_tm in line:
# do something with the line
print(line)
elif not one_time_done:
if not one_time.test_ID and test_ID in line:
# do something with the line
one_time.test_ID = 1
print(line)
elif not one_time.program_n and program_n in line:
# do something with the line
one_time.program_n = 1
print(line)
elif not one_time.prod_n and prod_n in line:
# do something with the line
one_time.prod_n = 1
print(line)
one_time_done = one_time.all_done()
The OneTime class's role is just to host variables indicating whether one of the one-time printed lines have already been printed. You could use a dict, or just plain variables to keep track of that.
The statement inside the any(...) function creates a generator which produces items when iterated over. It's like a list that you get values from one-by-one, except it's lazy. The items aren't stored inside it, but read in as-needed when any() asks for each line until it hits one that evaluates to True.
I know it seems like advanced stuff when you're new to Python and trying to learn. I'll give a brief explanation and provide a link below if you want to learn more.
In a nutshell, I'm not sure if you've become familiar with "list comprehensions" yet, but I'm sure you've seen them. They look like for loops inside square braces and produce list items:
>>> li = [char for char in "abcdefghijklmnop"]
>>> li
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
This ^ would create a list of characters, since when you iterate over a string, it produces one character at a time.
A generator looks like a list comprehension inside parenthesis (instead of square braces) and has the same effect, but the characters aren't produced until asked for:
>>> gen = (char for char in "abcdefghijklmnop")
>>> gen
<generator object <genexpr> at 0x7f9a447aec10>
>>> next(gen)
'a'
So, back to any(). It will pull items from the generator until it sees something that evaluates to True and then it'll stop. It's only looking for any value that's True in a sequence of items.
>>> any([False, False, False, True])
True
>>>
>>> any("foo" in line for line in ["bar", "baz", "foo", "qux"])
True
When iteration hits "foo", the expression "foo" in line is True. Iteration stops there. The whole statement in the parameter list of any() is a generator - sometimes they don't need their own parenthesis when used as a parameter.
In much the same way as the last example, the file generator will read in one line at a time until any() is satisfied - and it'll go no further. So in that way it's somewhat efficient.
Here's a basic tutorial if you're interested: https://www.csestack.org/python-fibonacci-generator/.

Getting an IndexError: list index out of range in line no. = 19?

import csv
with open('C:/Users/dkarar/Desktop/Mapping project/RC_Mapping.csv', 'r') as file1:
with open('C:/Users/dkarar/Desktop/Mapping project/Thinclient_mapping.csv', 'r') as file2:
with open('C:/Users/dkarar/Desktop/Mapping project/output.csv', 'w') as outfile:
writer = csv.writer(outfile)
reader1 = csv.reader(file1)
reader2 = csv.reader(file2)
for row in reader1:
if not row:
continue
for other_row in reader2:
if not other_row:
continue
# if we found a match, let's write it to the csv file with the id appended
if row[1].lower() == other_row[1].lower():
new_row = other_row
new_row.append(row[0])
writer.writerow(new_row)
continue
# reset file pointer to beginning of file
file2.seek(0)
You seem to be getting at least one row where there is a single element. That's why when accessing row[1] you get an IndexError, there's only one element in the list row.

Recognize a string in a txt from two columns

I want my program to read lines in a txt and to recognize a string using two columns, i tried with for row in column1 and column2 but it isn't working and i don't really know why, here is the code.(Here i want to print the 5 first letters when it recognize the string, but later i will put those five letters in a list).
import pandas as pd
import re
import numpy as np
link = "excelfilett.txt"
file = open(link, "r")
frames_load = []
is_count_frames_load = False
for line in file:
if "[Interface1]" in line:
is_count_frames_load = True
if is_count_frames_load== True:
frames_load.append(line)
if "[EthernetComNeed]" in line:
break
number_of_rows_load = len(frames_load) -1
header_load = re.split(r'\t', frames_load[0])
number_of_columns_load = len(header_load)
frame_array_load = np.full((number_of_rows_load, number_of_columns_load), 0)
df_frame_array_load = pd.DataFrame(frame_array_load)
df_frame_array_load.columns= header_load
for row in range(number_of_rows_load):
frame_row_load = re.split(r'\t', frames_load[row])
for position in range(len(frame_row_load)):
df_frame_array_load.iloc[row, position] = frame_row_load[position]
print(df_frame_array_load)
df_frame_array_load["[Name]"] = df_frame_array_load["[End1]"] + '\t' + df_frame_array_load["[End2]"]
df_frame_array_load["[Name2]"] = df_frame_array_load["[End2]"] + '\t' + df_frame_array_load["[End1]"]
print(df_frame_array_load["[Name]"])
print(df_frame_array_load["[Name2]"])
link = "excelfilett.txt"
file = open(link, "r")
frames_path = []
is_count_frames_path = False
for line in file:
if "[Routing Paths]" in line:
is_count_frames_path = True
if is_count_frames_path== True:
for row in df_frame_array_load["[Name]"] and df_frame_array_load["[Name2]"]:
if row in line:
print(row)
print(line[0:4])
if "[EthernetComNeed]" in line:
break
if "[EthernetComConfig]" in line:
break
What I want as output is to print the 5 first letters in the lines of the txt. I'm using when it recognize a string, for example, when "S1\tS2" is in the line of the txt, it will print me the 5 first letters, so "FL_1", the two columns contains string as "S1\tS2" and the inverse (like "S2\tS1"), it is the point of the line where I have an issue, it gives me
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
in the line "for row in column1 and column2:".
I think it isn't possible to read rows on two columns at one time, i just make my program read the two columns separately, here is what i changed:
link = "excelfilett.txt"
file = open(link, "r")
frames_path = []
is_count_frames_path = False
for line in file:
if "[Routing Paths]" in line:
is_count_frames_path = True
if is_count_frames_path== True:
for row in df_frame_array_load["[Name]"]:
if row in line:
print(row)
print(line[0:4])
if "[EthernetComNeed]" in line:
break
for row in df_frame_array_load["[Name2]"]:
if row in line:
print(row)
print(line[0:4])
if "[EthernetComConfig]" in line:
break

Program doesn't stop iterating through list

def gameinfo():
lines = []
html_doc = 'STATIC.html'
soup = BeautifulSoup(open(html_doc), 'html.parser')
for mytable in soup.find_all('table'):
for trs in mytable.find_all('tr'):
tds = trs.find_all('td')
row1 = [elem.text.strip() for elem in tds]
row = str(row1)
sausage = False
with open("FIRE.txt", "r+") as file:
for line in file:
if row+"\n" in line:
break
else:
if row.split(",")[:4] == line.split(",")[:4]:
print(row)
print(line)
file.write(line.replace(line+"\n", row+"\n"))
print('Already exists with diff date')
sausage = True
break
if sausage == False:
print(row.split(",")[:4])
print(line.split(",")[:4])
print(row)
print(line)
file.write(row+"\n")
print('appended')
while True:
gameinfo()
gameinfo()
This program is supposed to keep searching the text file FIRE.txt for lines that match the variable row. When i run it, it works okay, but the part of the code that is supposed to check if the first four elements of the list are the same, and then skin the appending section below, doesn't work. When the program detects that the first 4 elements of a string turned into a list(row) that matches with another string's first 4 elements that's in the text file, it should overwrite the string in the text file. However when it detects a list that has the same first 4 elements, it loops forever and never breaks out.
My string looks like this:
['Infield Upper Deck Reserved 529', '$17.29', '4', '2', '175']
and i compare it to a list that looks like this:
['Infield Upper Deck Reserved 529', '$17.29', '4', '2', '170']
and when it sees that the first 4 elements in the list are the same, it should overwrite the one that was in the text file to begin with, but it is looping.
Question has changed; most recent version last.
Methinks you want to use the csv module. If you iterate through a csv.reader object instead of the file object directly, you'll get each line as a a list.
Example:
import csv
row = ["this", "is", "an", "example"]
with open("FIRE.txt", "r+") as file:
reader = csv.reader(file)
for line in reader:
if row in line:
break
pass
Alternatively, if you don't need to use this in anything other than Python, you could pickle a collections.OrderedDict with a tuple of the first four items as the keys:
import collections
import pickle
import contextlib
#contextlib.contextmanager
def mutable_pickle(path, default=object):
try:
with open(path, "rb") as f:
obj = pickle.load(f)
except IOError, EOFError:
obj = default()
try:
yield obj
finally:
with open(path, "wb") as f:
pickle.dump(obj, f)
with mutable_pickle("fire.bin",
default=collections.OrderedDict) as d:
for row in rows:
d[tuple(row[:4])] = row

I need to find a line of text in python and copy it, and the lines above and below it

Basically, I need to find a line of text in python (and tk) and copy it, and the lines above and below it.
Imported_File = a long list of strings.
One_Ring = the line I'm looking for.
Find One_ring in Imported_File, Copy One_ring, the line above it and below it to Output_Var
I'm really stuck, any help would be amazing.
Considering you are reading from a file which has these long list of strings.
with open('textFile.txt') as f:
Imported_File = f.readlines()
One_Ring = "This is the line\n"
Output_Var = ""
if One_Ring in Imported_File:
idx = Imported_File.index(One_Ring)
Output_Var = Imported_File[max(0, idx - 1):idx+2]
print(Output_Var)
The textFile.txt would be your input file. I created a sample file which looks like
Hello there
How are you
This is the line
Done, Bye
I assume that you are looking the exact line
imported_file = ['a', 'b', 'c', 'd', 'e', 'f']
one_ring = 'd'
if one_ring in imported_file:
i = imported_file.index(one_ring)
start = i-1
end = i+2
# problem with first element before "a" when `one_ring = 'a'`
if start < 0:
start = 0
output_var = imported_file[start:end]
print(output_var)
else:
print('not found')
BTW: use lower_case names for varaibles: PEP 8 -- Style Guide for Python Code

Resources