basic python file IO homework - python

I'm having trouble figuring out where I'm going wrong here.
The original file is:
python is a programming language that lets you WORK more quickly and integrate your systems more effectively.
you can learn to use python and see almost immediate gains in PRODUCTIVITY and lower maintenance COSTS.
it's very helpful for any field of study.
I'm trying to create a function that takes a file and reads it and then capitalizes the sentences, changes the caps lock to lower case and the "it's" to "this is". Then put the file back together and add a period after the sentences. Write the new file string into a .txt file named 'Edited.txt.
My code is:
def edit(aFile):
f = open(aFile, 'r')
xs = f.readlines()
f.close()
g = open('happy.txt', 'w')
for x in xs:
x.capitalize()
if x.isupper==1:
x.lower()
g.write(x)
g.close()
The error I get is "File not found-happy.txt(Access is denied). I tried to read the file and couldn't.
I am 100% positive that the file is there and the media path is set to the folder.

isupper
is a method that returns True or False, so the line should read:
if x.isupper():
not
if x.isupper==1:
Not sure if this answers your question, but you should really post more about the error for us to answer properly.
Additionally, many of the python string methods, such as capitalize() and lower() create COPIES of the string, and don't actually modify the original string. So if:
x = "TEST"
then calling
y = x.lower()
will result in x still being "TEST" and y being "test".

This statement doesn't do anything as is:
x.capitalize()
It returns x with the first character capitalized, but you don't save the results anywhere. Also, x remains unchanged after this statement. If you want to capitalize the first char of x, do this:
x = x.capitalize()

The first major mistake that I can see is that you are doing string methods without assigning them to anything. Strings are immutable, so x.capitalize() does nothing (as jh314 said).
In addition to what the others have said, your for x in xs line is saying "for every line in the file, do the following". Your file appears to only be one line, so you are trying to do everything on one line.
Try looking at the documentation on regular expressions and string methods.
http://docs.python.org/2/library/string.html
http://docs.python.org/2/library/re.html
They should be helpful for identifying the places within your line that you would like to modify.

Related

Python writing int into file

I have a problem with python code and i don't know what to do, because im fairly new in it.
date_now1 = datetime.datetime.now()
archive_date1 = date_now1.strftime("%d.%m.%Y")
f1 = open(archive_date1, "r+")
print("What product do you wish to delete ?")
delate_product = str(input())
for line in f1.readlines():
if delate_product in line:
list = line
ready_product = list.split()
quantity_of_product = int(ready_product[1])
if quantity_of_product == 1:
del line
print("Product deleted")
else:
print("You have {} amounts of this product. How many do you want to delete ?".format(quantity_of_product))
x = int(input())
quantity_of_product = quantity_of_product - x
temporary = "{}".format(quantity_of_product)
print(type(temporary))
f1.write(temporary) in ready_product[1]
I get the message
f1.write(temporary) in ready_product[1]
TypeError: 'in <string>' requires string as left operand, not int
When i do print(type()) in temporary it says string. I also tried str(quantity_of_product), but it doesn't work as well. Maybe somebody could give me the idea of what to do, or what to read to get the answer.
The error is arising because you are asking python to find out whether an integer is "in" a string.
The output of f1.write(temporary) is an integer. To see this, try adding a print statement before the erroneous line. In contrast, ready_product[1] is a string (i.e. the second string element in the list "ready_product").
The operator "in" takes two iterables and returns whether the first is "in" the second. For example:
>>> "hello in ["hello", "world"]
>> True
>>> "b" in "a string"
>> False
When Python attempts to see if an integer is "in" a string, it cannot and throws a TypeError, saying "requires string as left operand, not int". This is the root of your error.
You may also have a number of other errors in your code:
"list" is a reserved word in Python, and so calling your variable "list" is bad practice. Try another name such as _list (or delete the variable as it doesn't appear to serve a purpose).
"del line" deletes the variable "line". However, it won't delete the actual line in the text file, only the variable containing it. See Deleting a specific line in a file (python) for how to delete a line from a text file.
There doesn't appear to be a f1.close() statement in the code. This is necessary to close the file after use, as otherwise edits may not be saved.
Personally, instead of attempting to delete lines as I go, I'd maintain a list of lines in the text file, and delete/alter lines from the list as I go. Then, at the end of the program I'd rewrite the file from the list of altered lines.

Print on a single line not working Python 3

I'm very new to programming and am working on some code to extract data from a bunch of text files. I've been able to do this however the data is not useful to me in Excel. Therefore, I would like to print it all on a single line and separate it by a special character, which I can then delimit in Excel.
Here is my code:
import os
data=['Find me','find you', 'find us']
with open('C:\\Users\\Documents\\File.txt', 'r') as inF:
for line in inF:
for a in data:
string=a
if string in line:
print (line,end='*') #print on same line
inF.close()
So basically what I'm doing is finding if a keyword is on that line and then printing that line if it is.
Even though I have print(,end='*'), I don't get the print on a single line. It outputs:
Find me
*find you
*find us
Where is the problem? (I'm using Python 3.5.1)
Your immediate problem is that you're not removing the newline characters from your lines before printing them. The usual way to do this is with strip(), eg:
print(line.strip(), end='*')
You'll also print multiple copies of the line if more than one of your special phrases appear in the line. To avoid that, add a break statement after your print, or (better, but a more advanced construct that might not make sense until you're used to generator expressions) use if any(keyword in line for keyword in data):
You also don't need to explicitly close the input file - the point of the with open(...) as ...: context manager is that it closes the file when exiting it.
And I would avoid using string as a variable name - it doesn't tell anyone anything about what the variable is used for, and it can cause confusion if you end up using the built-in string module for anything. It's not as bad as shadowing a built-in constructor like list, but it's worth avoiding. Especially since it does nothing for you here, you can just use if a in line: here if you don't want to use the any() version above.
In addition to all that, if your data is not extremely large (and I hope it's not if you're trying to fit it all on one line) you'll get tidier code and avoid the trailing delimiter by using the .join() method on strings, eg something like:
import os
data=['Find me','find you', 'find us']
with open('C:\\Users\\Documents\\File.txt', 'r') as inF:
print "*".join(line.strip() for line in inF if any(keyword in line for keyword in data))

Parsing a file in python

Caveat emptor: I can spell p-y-t-h-o-n and that's pretty much all there is to my knowledge. I tried to take some online classes but after about 20 lectures learning not much, I gave up long time ago. So, what I am going to ask is very simple but I need help:
I have a file with the following structure:
object_name_here:
object_owner:
- me#my.email.com
- user#another.email.com
object_id: some_string_here
identification: some_other_string_here
And this block repeats itself hundreds of times in the same file.
Other than object_name_here being unique and required, all other lines may or may not be present, email addresses can be from none to 10+ different email addresses.
what I want to do is to export this information into a flat file, likes of /etc/passwd, with a twist
for instance, I want the block above to yield a line like this:
object_name_here:object_owner=me#my_email.com,user#another.email.com:objectid=some_string_here:identification=some_other_string_here
again, the number of fields or length of the content fields are not fixed by any means. I am sure this is pretty easy task to accomplish with python but how, I don't know. I don't even know where to start from.
Final Edit: Okay, I am able to write a shell script (bash, ksh etc.) to parse the information, but, when I asked this question originally, I was under the impression that, python had a simpler way of handling uniform or semi-uniform data structures as this one. My understanding was proven to be not very accurate. Sorry for wasting your time.
As jaypb points out, regular expressions are a good idea here. If you're interested in some python 101, I'll give you some simple code to get you started on your own solution.
The following code is a quick and dirty way to lump every six lines of a file into one line of a new file:
# open some files to read and write
oldfile = open("oldfilename","r")
newfile = open("newfilename","w")
# initiate variables and iterate over the input file
count = 0
outputLine = ""
for line in oldfile:
# we're going to append lines in the file to the variable outputLine
# file.readline() will return one line of a file as a string
# str.strip() will remove whitespace at the beginning and end of a string
outputLine = outputLine + oldfile.readline().strip()
# you know your interesting stuff is six lines long, so
# reset the output string and write it to file every six lines
if count%6 == 0:
newfile.write(outputLine + "\n")
outputLine = ""
# increment the counter
count = count + 1
# clean up
oldfile.close()
newfile.close()
This isn't exactly what you want to do but it gets you close. For instance, if you want to get rid of " - " from the beginning of the email addresses and replace it with "=", instead of just appending to outputLine you'd do something like
if some condition:
outputLine = outputLine + '=' + oldfile.readline()[3:]
that last bit is a python slice, [3:] means "give me everything after the third element," and it works for things like strings or lists.
That'll get you started. Use google and the python docs (for instance, googling "python strip" takes you to the built-in types page for python 2.7.10) to understand every line above, then change things around to get what you need.
Since you are replacing text substrings with different text substrings, this is a pretty natural place to use regular expressions.
Python, fortunately, has an excellent regular expressions library called re.
You will probably want to heavily utilize
re.sub(pattern, repl, string)
Look at the documentation here:
https://docs.python.org/3/library/re.html
Update: Here's an example of how to use the regular expression library:
#!/usr/bin/env python
import re
body = None
with open("sample.txt") as f:
body = f.read()
# Replace emails followed by other emails
body = re.sub(" * - ([a-zA-Z.#]*)\n * -", r"\1,", body)
# Replace declarations of object properties
body = re.sub(" +([a-zA-Z_]*): *[\n]*", r"\1=", body)
# Strip newlines
body = re.sub(":?\n", ":", body)
print (body)
Example output:
$ python example.py
object_name_here:object_owner=me#my.email.com, user#another.email.com:object_id=some_string_here:identification=some_other_string_here

Spell check program in python

Exercise problem: "given a word list and a text file, spell check the
contents of the text file and print all (unique) words which aren't
found in the word list."
I didn't get solutions to the problem so can somebody tell me how I went and what the correct answer should be?:
As a disclaimer none of this parses in my python console...
My attempt:
a=list[....,.....,....,whatever goes here,...]
data = open(C:\Documents and Settings\bhaa\Desktop\blablabla.txt).read()
#I'm aware that something is wrong here since I get an error when I use it.....when I just write blablabla.txt it says that it can't find the thing. Is this function only gonna work if I'm working off the online IVLE program where all those files are automatically linked to the console or how would I do things from python without logging into the online IVLE?
for words in data:
for words not in a
print words
wrong = words not in a
right = words in a
print="wrong spelling:" + "properly splled words:" + right
oh yeh...I'm very sure I've indented everything correctly but I don't know how to format my question here so that it doesn't come out as a block like it has. sorry.
What do you think?
There are many things wrong with this code - I'm going to mark some of them below, but I strongly recommend that you read up on Python control flow constructs, comparison operators, and built-in data types.
a=list[....,.....,....,whatever goes here,...]
data = open(C:\Documents and Settings\bhaa\Desktop\blablabla.txt).read()
# The filename needs to be a string value - put "C:\..." in quotes!
for words in data:
# data is a string - iterating over it will give you one letter
# per iteration, not one word
for words not in a
# aside from syntax (remember the colons!), remember what for means - it
# executes its body once for every item in a collection. "not in a" is not a
# collection of any kind!
print words
wrong = words not in a
# this does not say what you think it says - "not in" is an operator which
# takes an arbitrary value on the left, and some collection on the right,
# and returns a single boolean value
right = words in a
# same as the previous line
print="wrong spelling:" + "properly splled words:" + right
I don't know what you are trying to iterate over, but why don't you just first iterate over your words (which are in the variable a I guess?) and then for every word in a you iterate over the wordlist and check whether or not that word is in the wordslist.
I won't paste code since it seems like homework to me (if so, please add the homework tag).
Btw the first argument to open() should be a string.
It's simple really. Turn both lists into sets then take the difference. Should take like 10 lines of code. You just have to figure out the syntax on your own ;) You aren't going to learn anything by having us write it for you.

program for comparisons in python

I'm really new to programming (really, really new) and need help with the basics. I'm trying to write a program with python that will compare the contents of two .txt files, one a reference and the other the source. The contents are a simple random listing of names, and I want it to print out if there are any names in the source that are not in the reference.
I've looked at other stuff on this site but every time I tried it, the terminal would never actually give a result, even if there was a print command in the program.
I also have a hard time reading the language of a program and ascertaining it's exact function, so something with clear directions would be really appreciated.
As far as I have is:
ref = open("reference.txt")
sor = open("source.txt")
list1 = ref.read()
list2 = sor.read()
for i in list2:
if i != list:
print i
ref.close()
sor.close()
And when I try and run this, it says "expected an indented block"? at the 'print i' line. Why?
Please help me out, as I have to teach myself this stuff and am not doing too well.
Thanks.
If you are totally, completely new to programming then it will take you some time to be able to implement what you describe. Take a step back, pour yourself a beverage, and start here. Start at the beginning, and repeat each illustration until you understand.
http://docs.python.org/tutorial/
As previously mentioned, your inner if statement needs to be indented, as
for i in list2:
if i != list:
print i
This requires two indents because it is two nested blocks. As a basic rule of thumb, anywhere you're ending a line with a colon (:), you're starting a new code block, and should be indenting another level. This is so you can un-indent once to end the if block without ending the for block.
However, I doubt this will do what you want based on your description. It's likely you wanted something more like
sourceLines = set(sor.readLines())
for line in ref.readlines():
if line not in sourcelines:
print line
if blocks in python have to be indented, add another level of indent for your print i statement
for i in list2:
if i != list:
print i
These lines read the files as strings:
list1 = ref.read()
list2 = sor.read()
This loop iterates through the string one character at a time:
for i in list2:
This line compares the character to the list class:
if i != list:
I'll answer your indentation error first: you need another 4 spaces before the print statement. In Python indentation is important and you need to indent any block and dedent to end that block.
For your problem I am going to not give you written out code in advance but more of a flow on how to do it:
Create 2 sets (http://docs.python.org/library/stdtypes.html#set-types-set-frozenset)
Read both files into a seperate set (you can do this while iterating over a file and appending to your set).
Compare your two sets using the set1 - set2 syntax (see the link above) to show all items not common to both sets.
Hope you can make it work from this.
Now for the code:
with open('file1.txt') as file1:
set1 = set(line for line in file1)
with open('file2.txt') as file2:
set2 = set(line for line in file2)
print set1 - set2
This uses some principles you are probably not familiar with (look up: list comprehensions, generator comprehensions and the previously noted link about sets which are unique collections).

Categories