How to make a function so that it can search any string in a file?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am creating a software with a machine learning algorithm(in python) and I am currently setting up an environment that is taking data from a file.
The problem is that I can't find a way for the function to search for any string in the file, so that it can progress to search for specific strings.
I have created some lists that are empty every time the program starts up and are automatically filled(ex. data.append(data_from_file)) with the data from my file environment_data.txt.
I haven't tried much, because I genuinely don't know what to make it search for any string.
These are the lists.
tasks =
deadlines_month =
deadlines_week =
deadlines_days =
difficulty =
The file format is: It is a little bit faulty but it is a minor problem.
Task( ex. Maths)
Month( ex. 4)
Week(From how many weeks the month has ex. 3)
Days(Days of the week, ex. 4)
Difficulty(from 1 to 10, ex. 9)
This is the class that appends the lists:
class ArrayFix:
def __init__(self, num):
file = open("environment_data.txt", "r")
lines = file.readlines()
tasks.append(lines[num + 1])
deadlines_month.append(lines[num + 2])
deadlines_week.append(lines[num + 3])
deadlines_days.append(lines[num + 4])
difficulty.append(lines[num + 5])
file.close()
This is the function to search for string to append the lists.
for line in array_fix_num:
file = open("environment_data.txt", "r")
lines = file.readlines()
if str in file.read():
if str in lines[line]:
ArrayFix(line)
else:
pass
else:
file.close()
break
What can I put to search for any string in the place of str is on the function?
The expected results is for the lists to not be empty but have data from the file but instead they are still empty, because I don't know how to search for any string.
python file search
add a comment |
I am creating a software with a machine learning algorithm(in python) and I am currently setting up an environment that is taking data from a file.
The problem is that I can't find a way for the function to search for any string in the file, so that it can progress to search for specific strings.
I have created some lists that are empty every time the program starts up and are automatically filled(ex. data.append(data_from_file)) with the data from my file environment_data.txt.
I haven't tried much, because I genuinely don't know what to make it search for any string.
These are the lists.
tasks =
deadlines_month =
deadlines_week =
deadlines_days =
difficulty =
The file format is: It is a little bit faulty but it is a minor problem.
Task( ex. Maths)
Month( ex. 4)
Week(From how many weeks the month has ex. 3)
Days(Days of the week, ex. 4)
Difficulty(from 1 to 10, ex. 9)
This is the class that appends the lists:
class ArrayFix:
def __init__(self, num):
file = open("environment_data.txt", "r")
lines = file.readlines()
tasks.append(lines[num + 1])
deadlines_month.append(lines[num + 2])
deadlines_week.append(lines[num + 3])
deadlines_days.append(lines[num + 4])
difficulty.append(lines[num + 5])
file.close()
This is the function to search for string to append the lists.
for line in array_fix_num:
file = open("environment_data.txt", "r")
lines = file.readlines()
if str in file.read():
if str in lines[line]:
ArrayFix(line)
else:
pass
else:
file.close()
break
What can I put to search for any string in the place of str is on the function?
The expected results is for the lists to not be empty but have data from the file but instead they are still empty, because I don't know how to search for any string.
python file search
add a comment |
I am creating a software with a machine learning algorithm(in python) and I am currently setting up an environment that is taking data from a file.
The problem is that I can't find a way for the function to search for any string in the file, so that it can progress to search for specific strings.
I have created some lists that are empty every time the program starts up and are automatically filled(ex. data.append(data_from_file)) with the data from my file environment_data.txt.
I haven't tried much, because I genuinely don't know what to make it search for any string.
These are the lists.
tasks =
deadlines_month =
deadlines_week =
deadlines_days =
difficulty =
The file format is: It is a little bit faulty but it is a minor problem.
Task( ex. Maths)
Month( ex. 4)
Week(From how many weeks the month has ex. 3)
Days(Days of the week, ex. 4)
Difficulty(from 1 to 10, ex. 9)
This is the class that appends the lists:
class ArrayFix:
def __init__(self, num):
file = open("environment_data.txt", "r")
lines = file.readlines()
tasks.append(lines[num + 1])
deadlines_month.append(lines[num + 2])
deadlines_week.append(lines[num + 3])
deadlines_days.append(lines[num + 4])
difficulty.append(lines[num + 5])
file.close()
This is the function to search for string to append the lists.
for line in array_fix_num:
file = open("environment_data.txt", "r")
lines = file.readlines()
if str in file.read():
if str in lines[line]:
ArrayFix(line)
else:
pass
else:
file.close()
break
What can I put to search for any string in the place of str is on the function?
The expected results is for the lists to not be empty but have data from the file but instead they are still empty, because I don't know how to search for any string.
python file search
I am creating a software with a machine learning algorithm(in python) and I am currently setting up an environment that is taking data from a file.
The problem is that I can't find a way for the function to search for any string in the file, so that it can progress to search for specific strings.
I have created some lists that are empty every time the program starts up and are automatically filled(ex. data.append(data_from_file)) with the data from my file environment_data.txt.
I haven't tried much, because I genuinely don't know what to make it search for any string.
These are the lists.
tasks =
deadlines_month =
deadlines_week =
deadlines_days =
difficulty =
The file format is: It is a little bit faulty but it is a minor problem.
Task( ex. Maths)
Month( ex. 4)
Week(From how many weeks the month has ex. 3)
Days(Days of the week, ex. 4)
Difficulty(from 1 to 10, ex. 9)
This is the class that appends the lists:
class ArrayFix:
def __init__(self, num):
file = open("environment_data.txt", "r")
lines = file.readlines()
tasks.append(lines[num + 1])
deadlines_month.append(lines[num + 2])
deadlines_week.append(lines[num + 3])
deadlines_days.append(lines[num + 4])
difficulty.append(lines[num + 5])
file.close()
This is the function to search for string to append the lists.
for line in array_fix_num:
file = open("environment_data.txt", "r")
lines = file.readlines()
if str in file.read():
if str in lines[line]:
ArrayFix(line)
else:
pass
else:
file.close()
break
What can I put to search for any string in the place of str is on the function?
The expected results is for the lists to not be empty but have data from the file but instead they are still empty, because I don't know how to search for any string.
python file search
python file search
edited Jan 4 at 17:14
Angelos
asked Jan 4 at 17:08
AngelosAngelos
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
so you can open, read and then close a file and assign the lines to a variable:
file = open("environment_data.txt", "r")
lines = file.readlines()
file.close()
then you can apply some simple checking logic to fine the data you're looking for:
data_test_case = "cat"
for line in lines:
if data_test_case in line:
DoSomething(line)
you could implement a more complex model using Regex:
import re
line = "Jan 3 07:57:39 Kali sshd[1397]: Failed password for root from 172.16.12.55 port 34380 ssh2"
new_line = re.findall('^[a-zA-Z]+s+d+s+[d:]+', line)[0]
output :
'Jan 3 07:57:39'
is this the sort of thing you're thinking of?
Yeah. I am looking into the regex model you mentioned... Is it possible to make it to check for just string?
– Angelos
Jan 4 at 17:24
awesome, so what you could do is have multiple regex cases that search for the structures you're looking for, then pass them in to the arrays you specified. if you're happy with this answer feel free to accept it! if you need more info let me know !
– Brandon Bailey
Jan 4 at 17:27
Alright! Thank you I think I figured out, I need to create a new class with regex to check for strings!
– Angelos
Jan 4 at 17:31
good idea, nice one !
– Brandon Bailey
Jan 4 at 17:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54043282%2fhow-to-make-a-function-so-that-it-can-search-any-string-in-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
so you can open, read and then close a file and assign the lines to a variable:
file = open("environment_data.txt", "r")
lines = file.readlines()
file.close()
then you can apply some simple checking logic to fine the data you're looking for:
data_test_case = "cat"
for line in lines:
if data_test_case in line:
DoSomething(line)
you could implement a more complex model using Regex:
import re
line = "Jan 3 07:57:39 Kali sshd[1397]: Failed password for root from 172.16.12.55 port 34380 ssh2"
new_line = re.findall('^[a-zA-Z]+s+d+s+[d:]+', line)[0]
output :
'Jan 3 07:57:39'
is this the sort of thing you're thinking of?
Yeah. I am looking into the regex model you mentioned... Is it possible to make it to check for just string?
– Angelos
Jan 4 at 17:24
awesome, so what you could do is have multiple regex cases that search for the structures you're looking for, then pass them in to the arrays you specified. if you're happy with this answer feel free to accept it! if you need more info let me know !
– Brandon Bailey
Jan 4 at 17:27
Alright! Thank you I think I figured out, I need to create a new class with regex to check for strings!
– Angelos
Jan 4 at 17:31
good idea, nice one !
– Brandon Bailey
Jan 4 at 17:58
add a comment |
so you can open, read and then close a file and assign the lines to a variable:
file = open("environment_data.txt", "r")
lines = file.readlines()
file.close()
then you can apply some simple checking logic to fine the data you're looking for:
data_test_case = "cat"
for line in lines:
if data_test_case in line:
DoSomething(line)
you could implement a more complex model using Regex:
import re
line = "Jan 3 07:57:39 Kali sshd[1397]: Failed password for root from 172.16.12.55 port 34380 ssh2"
new_line = re.findall('^[a-zA-Z]+s+d+s+[d:]+', line)[0]
output :
'Jan 3 07:57:39'
is this the sort of thing you're thinking of?
Yeah. I am looking into the regex model you mentioned... Is it possible to make it to check for just string?
– Angelos
Jan 4 at 17:24
awesome, so what you could do is have multiple regex cases that search for the structures you're looking for, then pass them in to the arrays you specified. if you're happy with this answer feel free to accept it! if you need more info let me know !
– Brandon Bailey
Jan 4 at 17:27
Alright! Thank you I think I figured out, I need to create a new class with regex to check for strings!
– Angelos
Jan 4 at 17:31
good idea, nice one !
– Brandon Bailey
Jan 4 at 17:58
add a comment |
so you can open, read and then close a file and assign the lines to a variable:
file = open("environment_data.txt", "r")
lines = file.readlines()
file.close()
then you can apply some simple checking logic to fine the data you're looking for:
data_test_case = "cat"
for line in lines:
if data_test_case in line:
DoSomething(line)
you could implement a more complex model using Regex:
import re
line = "Jan 3 07:57:39 Kali sshd[1397]: Failed password for root from 172.16.12.55 port 34380 ssh2"
new_line = re.findall('^[a-zA-Z]+s+d+s+[d:]+', line)[0]
output :
'Jan 3 07:57:39'
is this the sort of thing you're thinking of?
so you can open, read and then close a file and assign the lines to a variable:
file = open("environment_data.txt", "r")
lines = file.readlines()
file.close()
then you can apply some simple checking logic to fine the data you're looking for:
data_test_case = "cat"
for line in lines:
if data_test_case in line:
DoSomething(line)
you could implement a more complex model using Regex:
import re
line = "Jan 3 07:57:39 Kali sshd[1397]: Failed password for root from 172.16.12.55 port 34380 ssh2"
new_line = re.findall('^[a-zA-Z]+s+d+s+[d:]+', line)[0]
output :
'Jan 3 07:57:39'
is this the sort of thing you're thinking of?
answered Jan 4 at 17:15
Brandon BaileyBrandon Bailey
449211
449211
Yeah. I am looking into the regex model you mentioned... Is it possible to make it to check for just string?
– Angelos
Jan 4 at 17:24
awesome, so what you could do is have multiple regex cases that search for the structures you're looking for, then pass them in to the arrays you specified. if you're happy with this answer feel free to accept it! if you need more info let me know !
– Brandon Bailey
Jan 4 at 17:27
Alright! Thank you I think I figured out, I need to create a new class with regex to check for strings!
– Angelos
Jan 4 at 17:31
good idea, nice one !
– Brandon Bailey
Jan 4 at 17:58
add a comment |
Yeah. I am looking into the regex model you mentioned... Is it possible to make it to check for just string?
– Angelos
Jan 4 at 17:24
awesome, so what you could do is have multiple regex cases that search for the structures you're looking for, then pass them in to the arrays you specified. if you're happy with this answer feel free to accept it! if you need more info let me know !
– Brandon Bailey
Jan 4 at 17:27
Alright! Thank you I think I figured out, I need to create a new class with regex to check for strings!
– Angelos
Jan 4 at 17:31
good idea, nice one !
– Brandon Bailey
Jan 4 at 17:58
Yeah. I am looking into the regex model you mentioned... Is it possible to make it to check for just string?
– Angelos
Jan 4 at 17:24
Yeah. I am looking into the regex model you mentioned... Is it possible to make it to check for just string?
– Angelos
Jan 4 at 17:24
awesome, so what you could do is have multiple regex cases that search for the structures you're looking for, then pass them in to the arrays you specified. if you're happy with this answer feel free to accept it! if you need more info let me know !
– Brandon Bailey
Jan 4 at 17:27
awesome, so what you could do is have multiple regex cases that search for the structures you're looking for, then pass them in to the arrays you specified. if you're happy with this answer feel free to accept it! if you need more info let me know !
– Brandon Bailey
Jan 4 at 17:27
Alright! Thank you I think I figured out, I need to create a new class with regex to check for strings!
– Angelos
Jan 4 at 17:31
Alright! Thank you I think I figured out, I need to create a new class with regex to check for strings!
– Angelos
Jan 4 at 17:31
good idea, nice one !
– Brandon Bailey
Jan 4 at 17:58
good idea, nice one !
– Brandon Bailey
Jan 4 at 17:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54043282%2fhow-to-make-a-function-so-that-it-can-search-any-string-in-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown