Author Topic: Searching thru a file code  (Read 3243 times)

0 Members and 1 Guest are viewing this topic.

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Searching thru a file code
« on: October 15, 2012, 07:48:32 pm »
Alright, i have my reasons for this particular piece of code so dont worry its not for nothin :P (it just may seem useless because i modified it into a simpler version so that it can be helped with)

However, it doesn't work and the error I get is not referring to any lines in the script:

Code: [Select]
#test2.py
import re
import sys

x=1
while x==x:
  with open("room.txt", mode="a", encoding="utf-8") as log:
    log.seek(0)
    for line in log:
      if not re.search(("apple"), line):
        log.write("grape")
        log.write("\n")

assume this is the contents of room.txt just for clerification

Code: [Select]
cherry
orange
strawberry
lemmon

(there is an empty line after "lemmon" so that it writes starting with that new blank line)

an explination of the code's usage is probably necessary so ill just explain that part.

It's basically in a loop (the whole 6 lines, assume its an infinite loop) and if it cant find the word "apple" in any of the lines in the file then write "grape" onto the last new line (in this case the line after "lemmon").

I know that this code as it is will be quite useless but that's *only* because I had to modify it so that there wouldnt be any confusion over external definitions. The coding structure is the exact same.
The important part in this is fixing the error.

So when I run this code, I get this error:

Code: [Select]
Traceback (most recent call last):
  File "C:\Users\Jacob\Desktop\bot\test2.py", line 8, in <module>
    for line in log:
  File "C:\Python30\lib\io.py", line 1739, in __next__
    line = self.readline()
  File "C:\Python30\lib\io.py", line 1813, in readline
    while self._read_chunk():
  File "C:\Python30\lib\io.py", line 1560, in _read_chunk
    input_chunk = self.buffer.read1(self._CHUNK_SIZE)
AttributeError: 'BufferedWriter' object has no attribute 'read1'
« Last Edit: October 15, 2012, 07:52:06 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline lkj

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +58/-1
    • View Profile
Re: Searching thru a file code
« Reply #1 on: October 15, 2012, 09:33:02 pm »
A file opened in "a" mode can't be read. Only r+ or a+ seem suitable for what you want to do.
Most likely a+ for your code.
« Last Edit: October 15, 2012, 09:49:33 pm by lkj »

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Searching thru a file code
« Reply #2 on: October 15, 2012, 09:47:59 pm »
A file opened in "a" mode can't be written to. Only r+ or a+ seem suitable for what you want to do.
Actually, a mode is appending, which is a form of writing.

However, files opened for a can't be read, only appended to. You need a+ to read and append. (The Python docs say that the modes with a plus sign at the end mean "updating," with is just a really unclear way to say "read and write.")

See http://stackoverflow.com/questions/1466000/python-open-built-in-function-difference-between-modes-a-a-w-w-and-r for more information.
« Last Edit: October 15, 2012, 09:51:37 pm by Deep Thought »




Offline lkj

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +58/-1
    • View Profile
Re: Searching thru a file code
« Reply #3 on: October 15, 2012, 09:49:22 pm »
Oops, I meant they can't be read.

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: Searching thru a file code
« Reply #4 on: October 15, 2012, 11:00:15 pm »
Unfortunately it didnt work.. how am I able to search for a certain word in a line? For example if i have a line that says "I like dogs" and another line that says "I like cats", how am I able to search for the one that says "cats" vs the one that says "dogs"? What would be the search pattern in re.search?  ???
« Last Edit: October 15, 2012, 11:00:27 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Searching thru a file code
« Reply #5 on: October 15, 2012, 11:33:03 pm »
You don't need regular expressions for that, and you probably shouldn't use regular expressions if you don't need it. If you're just matching a simple string, string.find is a lot faster.

If you really need regular expressions, the syntax for re.search is re.search(pattern, string, flags=0). pattern can just be a string. (Of course, if you have a compiled regex object, you can just call its search function without the first parameter.)
« Last Edit: October 15, 2012, 11:33:41 pm by Deep Thought »




Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: Searching thru a file code
« Reply #6 on: October 16, 2012, 02:57:12 pm »
Finally got it working :D all I needed was f.readlines() instead of that for loop :P
Loved this place, still the best producers of power metal, and sparked my dreams of coding.