#๐Ÿ”’ reading a file in python

82 messages ยท Page 1 of 1 (latest)

frank quail
#

anyone got any clue on how to change text in a text file like add the on the start of every line that starts with /
my guess is just read the file add the on the line that starts with / save the string in a variable then do a fresh write?

with open("file.txt", "w") as f:
    f.write(modified_string)

thoughts?

faint wrenBOT
#

@frank quail

Python help channel opened

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.

frank quail
#

oh really..

frank quail
limber crown
#

You would load it into a list of lines

#

and iterate the list

frank quail
#

its only 600 lines so it doesn't matter that much

limber crown
#

how big is the file?

frank quail
#

ig

limber crown
#

it will be lightning fast

frank quail
#

but it would check each line which is inefficient?

limber crown
#

it will literally happen in fractions of a second

#

that's pretty efficient

frank quail
#

im trying to be a better programmer ๐Ÿ˜”

#

writing fast code..

limber crown
#

what's wrong with fractions of a second?

#

0.1 seconds is too slow?

frank quail
#

but yeah i couldn't think of anything else other than iterating each line ๐Ÿ˜„

limber crown
#

iteration in python is very fast

frank quail
#

can i read and write both at the same time tho?

#

ooo maybe i could just zip through two open() objects?

limber crown
frank quail
#

one for read and one for write

limber crown
#
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

frank quail
#

yeah.. thats pretty much what i was gonna do too..

#

oo didn't know startswith

#

`line[0] == "/"

limber crown
#

you could also do line[0] == '/'

#

yeah

frank quail
#

like append the write

limber crown
#

do you want to overwrite the file?

frank quail
#

not a fresh rewrite

frank quail
limber crown
#

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

frank quail
#

yeeee

#

got it

frank quail
limber crown
#

!e

text = 'hello\nworld'
print(text)
print([text])
faint wrenBOT
limber crown
#

are you asking why you see the \n in the list but not when it's out of the list/

frank quail
#

its like x = ["item\n", "kekwww\n"] but

for i in x:
 .  prints item without the \n
limber crown
#

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

frank quail
limber crown
#

this is because of something called __repr__ though

limber crown
#

if you ever want to see the "raw" interpretation of a string, you can use repr()

#

!e

text = 'hello\nworld'
print(repr(text))
faint wrenBOT
limber crown
#

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
frank quail
#

nice

frank quail
#

but what if i actually wanna write "look at this \n <- its called a newline character" then what

limber crown
#

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

frank quail
#
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

limber crown
#

!e

text = r'hello\nworld'
print(repr(text))
faint wrenBOT
limber crown
frank quail
#

it doesn't include n..

limber crown
#

I did read().splitlines() which gets rid of the \n

#

that's why I did '\n'.join when I wrote it back to file

frank quail
#

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

limber crown
#

I haven't really done web scraping, I'm not sure

frank quail
#

could use pyautogui?

#

fair enough

#

just looked up bettertouchtool does that for you

faint wrenBOT
#
Python help channel closed for inactivity

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.