How can I read last 10 bytes from a text file? [closed] - python

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
Welcome to demofile.txt
This file is for testing purposes.
Good Luck!
Above Content Is In My Text File
How to read last 10 bytes from that text file? The expected output is:
Good Luck!

in_file.seek(-10, 2) # where 2 denotes the reference point being the end of the file
So then:
in_file = open("demofile.txt", "rb") # opening for [r]eading as [b]inary
in_file.seek(-10, 2)
s = in_file.read(10)
print(s)
OUTPUT:
b'Good Luck!'

Related

I have a text file which contains sets , how do i read them as sets when i load on python? [closed]

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)

How to compare each line of a text file with a statement? [closed]

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 2 years ago.
Improve this question
Let's say I have a text file of links and garbage which look like this:
www.facebook.com
www.google.com
123
fewfwfqwfq
Let's do a for loop and compare each line and if there is not a ".com" in the specific line, the line gets deleted. In that case '123' and 'fewfwfqwfq' gets deleted.
How can I achieve that?
good_lines = [l for l in links if '.com' in l]
with open('fname.txt') as fout:
[fout.write(g) for g in goodlines]

python regex with multiple separators [closed]

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
I am trying to separate all the images from the following string.
how can I get a list of images that start with "comp1/img_" and are either split by a "," or a ";"
/*jsonp*/jsonresp({"img_set":"comp1/img_23434;comp1/img_3243r43r,comp1/img_o43nfjr;comp1/img_wjfno43,comp1/img_nrejfner;comp1/img_jrenckerjv,comp1/img_23434k;comp1/img_rkfnk4n"},"fknreff\",");
so I would end up with a list like...
comp1/img_23434
comp1/img_3243r43r
comp1/img_o43nfjr
comp1/img_wjfno43
comp1/img_nrejfner
comp1/img_jrenckerjv
comp1/img_23434k
comp1/img_rkfnk4n
any help would be appreciated.
thanks
You can do this:
>>> data = '/*jsonp*/jsonresp({"img_set":"comp1/img_23434;comp1/img_3243r43r,comp1/img_o43nfjr;comp1/img_wjfno43,comp1/img_nrejfner;comp1/img_jrenckerjv,comp1/img_23434k;comp1/img_rkfnk4n"},"fknreff\",");'
>>> import re
>>> re.findall(r'comp1/img_[^;,"]+', data)
['comp1/img_23434', 'comp1/img_3243r43r', 'comp1/img_o43nfjr', 'comp1/img_wjfno43', 'comp1/img_nrejfner', 'comp1/img_jrenckerjv', 'comp1/img_23434k', 'comp1/img_rkfnk4n']

Python 2.7: set the value of a variable with a symbolic expression written in a text file [closed]

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())

Regex replacing part of link [closed]

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 8 years ago.
Improve this question
I need to write a regex, that will traverse through my file and replace each :
href='#!/api/API.
with
href='http://www.domain.com/API. . I currently have this kind of code, that will be run from console :
def replace(file, pattern, substring):
my_file = open(file)
for line in my_file:
my_file.write(line.replace(pattern, substring))
Will sending the given strings work in this case ?
So you want to replace #!/api with http://www.domain.com?
You can just do:
line.replace('#!/api','http://www.domain.com')
No need for regex.

Categories