Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Hi I was wondering how I could format a large text file by adding line breaks after certain characters or words. For instance, everytime a comma was in the paragraph could I use python to make this output an extra linebreak.
You can do using str.replace() in python. Check out the below code, replacing every , with ,\n.
string = ""
with open('test.txt','r') as myfile:
for line in myfile:
string += line.replace(",",",\n")
myfile.close()
myfile = open('test.txt','w')
myfile.write(string)
File before execution:
Hello World and again HelloWorld,sdjakljsljsfs,asdgrwcfdssaasf,sdfoieunvsfaf,asdasdafjslkj,
After Execution:
Hello World and again HelloWorld,
sdjakljsljsfs,
asdgrwcfdssaasf,
sdfoieunvsfaf,
asdasdafjslkj,
you can use the ''.replace() method like so:
'roses can be blue, red, white'.replace(',' , ',\n') gives
'roses can be blue,\n red,\n white' efectively inserting '\n' after every ,
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
My text file looks like this
{} /n {}/n
When I use readline, I get it as a list , like ['{}' , '{}]
how do I remove the string and read them as sets?
This code work for you.
with open('yourfile.txt','r') as file:
data = file.read().splitlines()
data = [set(a[1:-1].split(',')) for a in data]
print(data)
You can use eval also here, But using eval might be dangerous.
with open('yourfile.txt','r') as file:
data = file.read().splitlines()
data = [eval(a) for a in data]
print(data)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Let's say I have the following .txt file:
"StringA1","StringA2","StringA3"
"StringB1","StringB2","StringB3"
"StringC1","StringC2","StringC3"
And I want a nested list in the format:
nestedList = [["StringA1","StringA2","StringA3"],["StringB1","StringB2","StringB2"],["StringC1","StringC2","StringC3"]]
so I can access StringB2 for example like this:
nestedList[1][1]
What would be the best approach? I do not have a tremendous amount of data, maybe 100 lines at max, so I don't need a database or something
You can this sample code:
with open('file.txt') as f:
nestedList = [line.split(',') for line in f.readlines()]
print(nestedList[1][1])
file = open('a.txt').read()
file
l=[]
res = file.split('\n')
for i in range(len(res)):
l.append(res[i].split(','))
print(l[1][1])
Assuming your file name as a.txt is having data in same format as you specified in question, i.e newline seperated so that we can add nested list, and data inside is ,(comma) seperated. Above code will give you the right output.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Suppose there is a file which has 'qasd erty123' in it. I want to extract 123 in 'erty123' from the file. The length of 123 is in a input variable, therefore, unknown to me. I have tried
index=string.find('erty')+len('erty')
print('index')
Output:9
How to print after that?
You have to load file to memory and then you can search in string using find()
It display everything after erty
text = "qasd erty123"
index = text.find('erty')
index = index + len('erty')
print(text[index:])
If text is longer then it may need more work to get interested part.
It looks for 'erty' and next it looks for space after 'erty' - this is why I use start in find(). Without start it will find space before 'erty'
text = "qasd erty123 hello"
index = text.find('erty')
start = index + len('erty')
end = text.find(' ', start)
print(text[start:end])
More complex text may need to use regex.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Here's a copy of some of the lines (solution, pos and gloss) in my txt file:
solution: (كَتَبَ kataba) [katab-u_1]
pos: katab/VERB_PERFECT+a/PVSUFF_SUBJ:3MS
gloss: ___ + write + he/it <verb>
I would like to return the word 'katab' that inside the square brackets in first line and remove all staff and lines and number every things. I'm working on python 2.7
I tried to write this code:
pattern = re.compile("'(?P[^']+)':\s*(?P<root>[^,]*)\d+")
Whenever you think "I need to match a pattern", you should think "Regular Expressions" as a good starting point. See doco. It is a little trickier since the input file is unicode.
import re
import codecs
with codecs.open("test.unicode.txt","rb", "utf-8") as f:
words = []
for line in f.readlines():
matches = re.match(b"solution:.+\[(?P<word>\w+).*\]", line, flags=re.U)
if matches:
words.append(matches.groups()[0])
print(words)
Output:
[u'katab']
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a text file where I wrote a list of symbolic equations. I need to set the value of a variable with this list.
In other words I'm looking for a command that automatically "copy" all I have written in the text file and "paste" it into an expression such this:
a = all_it's_written_in_the_text_file
Is this possible?
with open(filename, "rt") as f:
a = eval(f.read())