#๐ reading a file in python
82 messages ยท Page 1 of 1 (latest)
@frank quail
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Pretty much, yeah
oh really..
thought there would be a more efficient solution that creating a sting that big?
its only 600 lines so it doesn't matter that much
how big is the file?
ig
I do this to files hundreds of thousands of lines long
it will be lightning fast
but it would check each line which is inefficient?
but yeah i couldn't think of anything else other than iterating each line ๐
iteration in python is very fast
can i read and write both at the same time tho?
ooo maybe i could just zip through two open() objects?
I would load the lines into a list then write the list out
one for read and one for write
with open('file.txt') as f:
lines = f.read().splitlines()
new_lines = []
for line in lines:
if line.startswith('/'):
line = 'the ' + line
new_lines.append(line)
with open('file.txt', 'w') as f:
f.write('\n'.join(new_lines))
that's the entire thing
I wrote it in 30 seconds and it will take python 0.1 seconds to run
yeah.. thats pretty much what i was gonna do too..
oo didn't know startswith
`line[0] == "/"
whats the char for write on top tho?
like append the write
do you want to overwrite the file?
not a fresh rewrite
just asking
so you basically want to double the file?
you can use "a" instead of "w"
it will append to the existing file instead of overwriting
btw if i do this it doesn't show a newline character
lines = []
with open("output1.txt", "r") as f:
for line in f.readlines():
print(line + "line ends")
if line[0] == "/":
lines.append(f"{main_url}{line}")
else:
lines.append(line)
print("end results:")
for line in lines:
print(line)
but if i do this it does show a newline character in list why?
print(lines)
!e
text = 'hello\nworld'
print(text)
print([text])
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | hello
002 | world
003 | ['hello\nworld']
are you asking why you see the \n in the list but not when it's out of the list/
its like x = ["item\n", "kekwww\n"] but
for i in x:
. prints item without the \n
look at my example. Strings display differently inside a list than outside
it would be chaos if newlines were parsed when strings were in a list
so it would be fine if i write it like this i wont get any newline characters in my file
this is because of something called __repr__ though
newline characters won't show up in the file. They'll be interpreted as actual newlines
if you ever want to see the "raw" interpretation of a string, you can use repr()
!e
text = 'hello\nworld'
print(repr(text))
:white_check_mark: Your 3.13 eval job has completed with return code 0.
'hello\nworld'
this is the python string literal though, that doesn't mean it's how it will be written to file
in file it will be
hello
world
nice
so if i do file.write("hello\nworld") the file would look like
hello
world
but what if i actually wanna write "look at this \n <- its called a newline character" then what
you have to escape it
an easy way is to create a raw string. It's a bit trickier if you already have the string created though
lines = []
with open("output1.txt", "r") as f:
for line in f.readlines():
print(line + "line ends")
if line[0] == "/":
lines.append(f"{main_url}{line}")
else:
lines.append(line)
with open("output2.txt", "w") as f:
f.write("".join(lines))
this works
!e
text = r'hello\nworld'
print(repr(text))
:white_check_mark: Your 3.13 eval job has completed with return code 0.
'hello\\nworld'
yeah, you don't really have to handle the newlines if you use readlines()
it doesn't include n..
I did read().splitlines() which gets rid of the \n
that's why I did '\n'.join when I wrote it back to file
btw this is offtopic do you know how can i search something?
like any text
open firefox automatically and paste in url
one way could be to just use selenium?
but i want something like a keyboard shortcut
select any text + keybinding => and boom search it
I haven't really done web scraping, I'm not sure
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.