If between two different characters in a text file, Python2019 Community Moderator ElectionWhat is the difference between old style and new style classes in Python?How do I copy a file in Python?What is the difference between @staticmethod and @classmethod?Difference between append vs. extend list methods in PythonWhat's the difference between lists and tuples?Difference between __str__ and __repr__?What are the differences between type() and isinstance()?How to concatenate two lists in Python?Get difference between two listsHow do you append to a file in Python?
Why did it take so long to abandon sail after steamships were demonstrated?
The difference between「N分で」and「後N分で」
Min function accepting varying number of arguments in C++17
Happy pi day, everyone!
Why do passenger jet manufacturers design their planes with stall prevention systems?
Welcoming 2019 Pi day: How to draw the letter π?
Does Mathematica reuse previous computations?
Recruiter wants very extensive technical details about all of my previous work
How could a scammer know the apps on my phone / iTunes account?
Are there other languages, besides English, where the indefinite (or definite) article varies based on sound?
How to use of "the" before known matrices
In a future war, an old lady is trying to raise a boy but one of the weapons has made everyone deaf
Look at your watch and tell me what time is it. vs Look at your watch and tell me what time it is
(Calculus) Derivative Thinking Question
How to make healing in an exploration game interesting
Official degrees of earth’s rotation per day
Are ETF trackers fundamentally better than individual stocks?
Can I use USB data pins as power source
My Graph Theory Students
Python if-else code style for reduced code for rounding floats
What approach do we need to follow for projects without a test environment?
How Could an Airship Be Repaired Mid-Flight
Are all passive ability checks floors for active ability checks?
Why doesn't using two cd commands in bash script execute the second command?
If between two different characters in a text file, Python
2019 Community Moderator ElectionWhat is the difference between old style and new style classes in Python?How do I copy a file in Python?What is the difference between @staticmethod and @classmethod?Difference between append vs. extend list methods in PythonWhat's the difference between lists and tuples?Difference between __str__ and __repr__?What are the differences between type() and isinstance()?How to concatenate two lists in Python?Get difference between two listsHow do you append to a file in Python?
I am basically trying to use python for a find and replace, but make it only apply to strings between "s:" and the following ",". I have a long text file of many of the following:
["c", "DashedSentence", s: "Yo limpio mi cuarto todos los sábados.",
"Question", q: "¿Cuándo limpio mi cuarto?",
as: ["Todos los sábados.",
"Todos los domingos."]],
["c", "DashedSentence", s: "Nosotros contestamos el correo cada semana.",
"Question", q: "¿Con qué frecuencia contestamos el correo?",
as: ["Cada semana.",
"Cada dos semanas."]],
In the end, I want phrases grouped together by underscores within the "s:" sections, by replacing " mi " with " mi_" to yield "mi_cuarto", and similarly with "los" "el" ... and many more that aren't in the given examples.
All I have so far is:
s = open("stimuli.txt").read()
word = [' mi ','los ']
phrase = [' mi_',' los_']
for i in range(len(word)):
if BETWEEN "s:" and ",":
s = s.replace(word[i],phrase[i])
f = open("stimuli_phrases.txt", 'w')
f.write(file)
Of course, BETWEEN isn't real, that's what I'm looking for. I might not be approaching the problem the right way, so I'm also open to any alternative ideas! I appreciate the help, thanks!
edit: The desired output groups noun phrases and prepositional phrases with in the s: sections, like so:
["c", "DashedSentence", s: "Yo limpio mi_cuarto todos_los_sábados.",
"Question", q: "¿Cuándo limpio mi cuarto?",
as: ["Todos los sábados.",
"Todos los domingos."]],
["c", "DashedSentence", s: "Nosotros contestamos el_correo cada_semana.",
"Question", q: "¿Con qué frecuencia contestamos el correo?",
as: ["Cada semana.",
"Cada dos semanas."]],
python if-statement between
add a comment |
I am basically trying to use python for a find and replace, but make it only apply to strings between "s:" and the following ",". I have a long text file of many of the following:
["c", "DashedSentence", s: "Yo limpio mi cuarto todos los sábados.",
"Question", q: "¿Cuándo limpio mi cuarto?",
as: ["Todos los sábados.",
"Todos los domingos."]],
["c", "DashedSentence", s: "Nosotros contestamos el correo cada semana.",
"Question", q: "¿Con qué frecuencia contestamos el correo?",
as: ["Cada semana.",
"Cada dos semanas."]],
In the end, I want phrases grouped together by underscores within the "s:" sections, by replacing " mi " with " mi_" to yield "mi_cuarto", and similarly with "los" "el" ... and many more that aren't in the given examples.
All I have so far is:
s = open("stimuli.txt").read()
word = [' mi ','los ']
phrase = [' mi_',' los_']
for i in range(len(word)):
if BETWEEN "s:" and ",":
s = s.replace(word[i],phrase[i])
f = open("stimuli_phrases.txt", 'w')
f.write(file)
Of course, BETWEEN isn't real, that's what I'm looking for. I might not be approaching the problem the right way, so I'm also open to any alternative ideas! I appreciate the help, thanks!
edit: The desired output groups noun phrases and prepositional phrases with in the s: sections, like so:
["c", "DashedSentence", s: "Yo limpio mi_cuarto todos_los_sábados.",
"Question", q: "¿Cuándo limpio mi cuarto?",
as: ["Todos los sábados.",
"Todos los domingos."]],
["c", "DashedSentence", s: "Nosotros contestamos el_correo cada_semana.",
"Question", q: "¿Con qué frecuencia contestamos el correo?",
as: ["Cada semana.",
"Cada dos semanas."]],
python if-statement between
You might want to study there
module: docs.python.org/2/library/re.html
– Haroldo_OK
Mar 7 at 14:04
1
Please show expected result.
– Alderven
Mar 7 at 14:04
Youras
examples are confusing and do not match thes
strings. This can be solved with regular expressions, but theas
phrases would require some level of natural language processing to yield that type of result.
– Aaron Ciuffo
Mar 7 at 14:46
The whole text file is to be used on an internet based experiment platform called Ibex Farm. I don't know what is supposed to match between as phrases and s strings, but the format I have presented is exactly what it needs to be for the platform I'm using, and it works without any issue (i.e. the question is given with two clickable answers below).
– Danny kun
Mar 8 at 2:24
add a comment |
I am basically trying to use python for a find and replace, but make it only apply to strings between "s:" and the following ",". I have a long text file of many of the following:
["c", "DashedSentence", s: "Yo limpio mi cuarto todos los sábados.",
"Question", q: "¿Cuándo limpio mi cuarto?",
as: ["Todos los sábados.",
"Todos los domingos."]],
["c", "DashedSentence", s: "Nosotros contestamos el correo cada semana.",
"Question", q: "¿Con qué frecuencia contestamos el correo?",
as: ["Cada semana.",
"Cada dos semanas."]],
In the end, I want phrases grouped together by underscores within the "s:" sections, by replacing " mi " with " mi_" to yield "mi_cuarto", and similarly with "los" "el" ... and many more that aren't in the given examples.
All I have so far is:
s = open("stimuli.txt").read()
word = [' mi ','los ']
phrase = [' mi_',' los_']
for i in range(len(word)):
if BETWEEN "s:" and ",":
s = s.replace(word[i],phrase[i])
f = open("stimuli_phrases.txt", 'w')
f.write(file)
Of course, BETWEEN isn't real, that's what I'm looking for. I might not be approaching the problem the right way, so I'm also open to any alternative ideas! I appreciate the help, thanks!
edit: The desired output groups noun phrases and prepositional phrases with in the s: sections, like so:
["c", "DashedSentence", s: "Yo limpio mi_cuarto todos_los_sábados.",
"Question", q: "¿Cuándo limpio mi cuarto?",
as: ["Todos los sábados.",
"Todos los domingos."]],
["c", "DashedSentence", s: "Nosotros contestamos el_correo cada_semana.",
"Question", q: "¿Con qué frecuencia contestamos el correo?",
as: ["Cada semana.",
"Cada dos semanas."]],
python if-statement between
I am basically trying to use python for a find and replace, but make it only apply to strings between "s:" and the following ",". I have a long text file of many of the following:
["c", "DashedSentence", s: "Yo limpio mi cuarto todos los sábados.",
"Question", q: "¿Cuándo limpio mi cuarto?",
as: ["Todos los sábados.",
"Todos los domingos."]],
["c", "DashedSentence", s: "Nosotros contestamos el correo cada semana.",
"Question", q: "¿Con qué frecuencia contestamos el correo?",
as: ["Cada semana.",
"Cada dos semanas."]],
In the end, I want phrases grouped together by underscores within the "s:" sections, by replacing " mi " with " mi_" to yield "mi_cuarto", and similarly with "los" "el" ... and many more that aren't in the given examples.
All I have so far is:
s = open("stimuli.txt").read()
word = [' mi ','los ']
phrase = [' mi_',' los_']
for i in range(len(word)):
if BETWEEN "s:" and ",":
s = s.replace(word[i],phrase[i])
f = open("stimuli_phrases.txt", 'w')
f.write(file)
Of course, BETWEEN isn't real, that's what I'm looking for. I might not be approaching the problem the right way, so I'm also open to any alternative ideas! I appreciate the help, thanks!
edit: The desired output groups noun phrases and prepositional phrases with in the s: sections, like so:
["c", "DashedSentence", s: "Yo limpio mi_cuarto todos_los_sábados.",
"Question", q: "¿Cuándo limpio mi cuarto?",
as: ["Todos los sábados.",
"Todos los domingos."]],
["c", "DashedSentence", s: "Nosotros contestamos el_correo cada_semana.",
"Question", q: "¿Con qué frecuencia contestamos el correo?",
as: ["Cada semana.",
"Cada dos semanas."]],
python if-statement between
python if-statement between
edited Mar 7 at 14:10
Danny kun
asked Mar 7 at 14:01
Danny kunDanny kun
204
204
You might want to study there
module: docs.python.org/2/library/re.html
– Haroldo_OK
Mar 7 at 14:04
1
Please show expected result.
– Alderven
Mar 7 at 14:04
Youras
examples are confusing and do not match thes
strings. This can be solved with regular expressions, but theas
phrases would require some level of natural language processing to yield that type of result.
– Aaron Ciuffo
Mar 7 at 14:46
The whole text file is to be used on an internet based experiment platform called Ibex Farm. I don't know what is supposed to match between as phrases and s strings, but the format I have presented is exactly what it needs to be for the platform I'm using, and it works without any issue (i.e. the question is given with two clickable answers below).
– Danny kun
Mar 8 at 2:24
add a comment |
You might want to study there
module: docs.python.org/2/library/re.html
– Haroldo_OK
Mar 7 at 14:04
1
Please show expected result.
– Alderven
Mar 7 at 14:04
Youras
examples are confusing and do not match thes
strings. This can be solved with regular expressions, but theas
phrases would require some level of natural language processing to yield that type of result.
– Aaron Ciuffo
Mar 7 at 14:46
The whole text file is to be used on an internet based experiment platform called Ibex Farm. I don't know what is supposed to match between as phrases and s strings, but the format I have presented is exactly what it needs to be for the platform I'm using, and it works without any issue (i.e. the question is given with two clickable answers below).
– Danny kun
Mar 8 at 2:24
You might want to study the
re
module: docs.python.org/2/library/re.html– Haroldo_OK
Mar 7 at 14:04
You might want to study the
re
module: docs.python.org/2/library/re.html– Haroldo_OK
Mar 7 at 14:04
1
1
Please show expected result.
– Alderven
Mar 7 at 14:04
Please show expected result.
– Alderven
Mar 7 at 14:04
Your
as
examples are confusing and do not match the s
strings. This can be solved with regular expressions, but the as
phrases would require some level of natural language processing to yield that type of result.– Aaron Ciuffo
Mar 7 at 14:46
Your
as
examples are confusing and do not match the s
strings. This can be solved with regular expressions, but the as
phrases would require some level of natural language processing to yield that type of result.– Aaron Ciuffo
Mar 7 at 14:46
The whole text file is to be used on an internet based experiment platform called Ibex Farm. I don't know what is supposed to match between as phrases and s strings, but the format I have presented is exactly what it needs to be for the platform I'm using, and it works without any issue (i.e. the question is given with two clickable answers below).
– Danny kun
Mar 8 at 2:24
The whole text file is to be used on an internet based experiment platform called Ibex Farm. I don't know what is supposed to match between as phrases and s strings, but the format I have presented is exactly what it needs to be for the platform I'm using, and it works without any issue (i.e. the question is given with two clickable answers below).
– Danny kun
Mar 8 at 2:24
add a comment |
1 Answer
1
active
oldest
votes
The file you gave is JSON formatted, which mean it could easily be parsed with the builtin python json library:
import json
with open("/path/to/your/file", "r") as f:
data = json.load(f)
for item in data:
try:
s = item['s']
except (TypeError, KeyError):
pass
Of course, if you do not want or can parse this file as json, you could use the re library:
import re
to_process = re.findall("s:"(.+)"", yourtext)
To learn or practice with regex, look at there: https://regexr.com/
Exactly, I just spotted that too. Thanks
– olinox14
Mar 7 at 14:13
Considering lack of quotes in object keys, I'd rather use yaml.
– bereal
Mar 7 at 14:14
Thanks so much for the JSON library recommendation. I need to study up on it a bit, to make it work... for now, copying and pasting the code you've provided (but with my file path), it's giving me the error -- JSONDecodeError: Expecting property name enclosed in double quotes -- but you're right that this is a .js file... kind of... it's a text file that I copy and paste into a larger .js file that is used by Ibex Farm for an online experiment. I'm not sure if their formatting requirements are exactly the same as JSON, but at least I've got a good place to start looking. Thanks!!
– Danny kun
Mar 8 at 2:26
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%2f55045606%2fif-between-two-different-characters-in-a-text-file-python%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
The file you gave is JSON formatted, which mean it could easily be parsed with the builtin python json library:
import json
with open("/path/to/your/file", "r") as f:
data = json.load(f)
for item in data:
try:
s = item['s']
except (TypeError, KeyError):
pass
Of course, if you do not want or can parse this file as json, you could use the re library:
import re
to_process = re.findall("s:"(.+)"", yourtext)
To learn or practice with regex, look at there: https://regexr.com/
Exactly, I just spotted that too. Thanks
– olinox14
Mar 7 at 14:13
Considering lack of quotes in object keys, I'd rather use yaml.
– bereal
Mar 7 at 14:14
Thanks so much for the JSON library recommendation. I need to study up on it a bit, to make it work... for now, copying and pasting the code you've provided (but with my file path), it's giving me the error -- JSONDecodeError: Expecting property name enclosed in double quotes -- but you're right that this is a .js file... kind of... it's a text file that I copy and paste into a larger .js file that is used by Ibex Farm for an online experiment. I'm not sure if their formatting requirements are exactly the same as JSON, but at least I've got a good place to start looking. Thanks!!
– Danny kun
Mar 8 at 2:26
add a comment |
The file you gave is JSON formatted, which mean it could easily be parsed with the builtin python json library:
import json
with open("/path/to/your/file", "r") as f:
data = json.load(f)
for item in data:
try:
s = item['s']
except (TypeError, KeyError):
pass
Of course, if you do not want or can parse this file as json, you could use the re library:
import re
to_process = re.findall("s:"(.+)"", yourtext)
To learn or practice with regex, look at there: https://regexr.com/
Exactly, I just spotted that too. Thanks
– olinox14
Mar 7 at 14:13
Considering lack of quotes in object keys, I'd rather use yaml.
– bereal
Mar 7 at 14:14
Thanks so much for the JSON library recommendation. I need to study up on it a bit, to make it work... for now, copying and pasting the code you've provided (but with my file path), it's giving me the error -- JSONDecodeError: Expecting property name enclosed in double quotes -- but you're right that this is a .js file... kind of... it's a text file that I copy and paste into a larger .js file that is used by Ibex Farm for an online experiment. I'm not sure if their formatting requirements are exactly the same as JSON, but at least I've got a good place to start looking. Thanks!!
– Danny kun
Mar 8 at 2:26
add a comment |
The file you gave is JSON formatted, which mean it could easily be parsed with the builtin python json library:
import json
with open("/path/to/your/file", "r") as f:
data = json.load(f)
for item in data:
try:
s = item['s']
except (TypeError, KeyError):
pass
Of course, if you do not want or can parse this file as json, you could use the re library:
import re
to_process = re.findall("s:"(.+)"", yourtext)
To learn or practice with regex, look at there: https://regexr.com/
The file you gave is JSON formatted, which mean it could easily be parsed with the builtin python json library:
import json
with open("/path/to/your/file", "r") as f:
data = json.load(f)
for item in data:
try:
s = item['s']
except (TypeError, KeyError):
pass
Of course, if you do not want or can parse this file as json, you could use the re library:
import re
to_process = re.findall("s:"(.+)"", yourtext)
To learn or practice with regex, look at there: https://regexr.com/
edited Mar 7 at 14:18
answered Mar 7 at 14:06
olinox14olinox14
1,244618
1,244618
Exactly, I just spotted that too. Thanks
– olinox14
Mar 7 at 14:13
Considering lack of quotes in object keys, I'd rather use yaml.
– bereal
Mar 7 at 14:14
Thanks so much for the JSON library recommendation. I need to study up on it a bit, to make it work... for now, copying and pasting the code you've provided (but with my file path), it's giving me the error -- JSONDecodeError: Expecting property name enclosed in double quotes -- but you're right that this is a .js file... kind of... it's a text file that I copy and paste into a larger .js file that is used by Ibex Farm for an online experiment. I'm not sure if their formatting requirements are exactly the same as JSON, but at least I've got a good place to start looking. Thanks!!
– Danny kun
Mar 8 at 2:26
add a comment |
Exactly, I just spotted that too. Thanks
– olinox14
Mar 7 at 14:13
Considering lack of quotes in object keys, I'd rather use yaml.
– bereal
Mar 7 at 14:14
Thanks so much for the JSON library recommendation. I need to study up on it a bit, to make it work... for now, copying and pasting the code you've provided (but with my file path), it's giving me the error -- JSONDecodeError: Expecting property name enclosed in double quotes -- but you're right that this is a .js file... kind of... it's a text file that I copy and paste into a larger .js file that is used by Ibex Farm for an online experiment. I'm not sure if their formatting requirements are exactly the same as JSON, but at least I've got a good place to start looking. Thanks!!
– Danny kun
Mar 8 at 2:26
Exactly, I just spotted that too. Thanks
– olinox14
Mar 7 at 14:13
Exactly, I just spotted that too. Thanks
– olinox14
Mar 7 at 14:13
Considering lack of quotes in object keys, I'd rather use yaml.
– bereal
Mar 7 at 14:14
Considering lack of quotes in object keys, I'd rather use yaml.
– bereal
Mar 7 at 14:14
Thanks so much for the JSON library recommendation. I need to study up on it a bit, to make it work... for now, copying and pasting the code you've provided (but with my file path), it's giving me the error -- JSONDecodeError: Expecting property name enclosed in double quotes -- but you're right that this is a .js file... kind of... it's a text file that I copy and paste into a larger .js file that is used by Ibex Farm for an online experiment. I'm not sure if their formatting requirements are exactly the same as JSON, but at least I've got a good place to start looking. Thanks!!
– Danny kun
Mar 8 at 2:26
Thanks so much for the JSON library recommendation. I need to study up on it a bit, to make it work... for now, copying and pasting the code you've provided (but with my file path), it's giving me the error -- JSONDecodeError: Expecting property name enclosed in double quotes -- but you're right that this is a .js file... kind of... it's a text file that I copy and paste into a larger .js file that is used by Ibex Farm for an online experiment. I'm not sure if their formatting requirements are exactly the same as JSON, but at least I've got a good place to start looking. Thanks!!
– Danny kun
Mar 8 at 2:26
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%2f55045606%2fif-between-two-different-characters-in-a-text-file-python%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
You might want to study the
re
module: docs.python.org/2/library/re.html– Haroldo_OK
Mar 7 at 14:04
1
Please show expected result.
– Alderven
Mar 7 at 14:04
Your
as
examples are confusing and do not match thes
strings. This can be solved with regular expressions, but theas
phrases would require some level of natural language processing to yield that type of result.– Aaron Ciuffo
Mar 7 at 14:46
The whole text file is to be used on an internet based experiment platform called Ibex Farm. I don't know what is supposed to match between as phrases and s strings, but the format I have presented is exactly what it needs to be for the platform I'm using, and it works without any issue (i.e. the question is given with two clickable answers below).
– Danny kun
Mar 8 at 2:24