File input output python save file - python

I'm trying to take a dictionary and write it to a file. Each line of output is suppose to contained the key and its value separated by a single space. So far I have the following:
def save(diction):
savefile = open("save.txt", "w")
for line in diction:
values = line.split()
savefile.write(values)
savefile.close()
I'm not sure if this is writing to the file or if it even saves the file with the .close() function. Any advice?

The values are being written to the file on savefile.write(values), however the method with which you are opening and closing the file is a bit dangerous. If you encounter an error the file may never be closed. It's better to use with to ensure that the file will automatically be closed when leaving the with block, whether by normal execution or on error. Also, you probably mean to iterate through diction.items()?
def save(diction):
with open("save.txt", "w") as savefile:
for key, value in diction.items():
values = "%s %s\n" % (key, value)
savefile.write(values)

If you try to call this function, it will give you an exception, like this:
----> 7 savefile.write(values)
TypeError: must be str, not list
… and then quit. So no, it's not writing anything, because there's an error in your code.
What does that error mean? Well, obviously something in that line is a list when it should be a str. It's not savefile, it's not write, so it must be values. And in fact, values is a list, because that's what you get back from split. (If it isn't obvious to you, add some debugging code into the function to print(values), or print(type(values)) and run your code again.)
If you look up the write method in the built-in help or on the web, or re-read the tutorial section Methods of File Objects, you will see that it does in fact require a string.
So, how do you write out a list?
You have to decide what you want it to look like. If you want it to look the same as it does in the print statement, you can just call str to get that:
savefile.write(str(values))
But usually, you want it to be something that looks nice to humans, or something that's easy to parse for later scripts you write.
For example, if you want to print out each values as a line made up of 20-character-wide columns, you could do something like this:
savefile.write(''.join(format(value, '<20') for value in values) + '\n')
If you want something you can parse later, it's usually better to use one of the modules that knows how to write (and read) specific formats—csv, json, pickle, etc.
The tutorial chapter on Input and Output is worth reading for more information.

def save_dict(my_dict):
with open('save.txt', 'w') as f:
for key in my_dict.keys():
f.write(key + ' ' + my_dict[key] + '\n')
if __name__ == "__main__":
my_dict = {'a': '1',
'b': '2'}
save_dict(my_dict)

def save(diction):
with open('save.txt', 'w') as savefile:
for key, value in diction.items():
savefile.write(str(key)+ ' ' + str(value) + '\n')

Related

Write a program in Python 3.5 that reads a file, then writes a different file with the same text that was in the first one as well as more?

The exact question to this problem is:
*Create a file with a 20 lines of text and name it “lines.txt”. Write a program to read this a file “lines.txt” and write the text to a new file, “numbered_lines.txt”, that will also have line numbers at the beginning of each line.
Example:
Input file: “lines.txt”
Line one
Line two
Expected output file:
1 Line one
2 Line two
I am stuck, and this is what I have so far. I am a true beginner to Python and my instructor does not make things very clear. Critique and help much appreciated.
file_object=open("lines.txt",'r')
for ln in file_object:
print(ln)
count=1
file_input=open("numbered_lines.txt",'w')
for Line in file_object:
print(count,' Line',(str))
count=+1
file_object.close
file_input.close
All I get for output is the .txt file I created stating lines 1-20. I am very stuck and honestly have very little idea about what I am doing. Thank you
You have all the right parts, and you're almost there:
When you do
for ln in file_object:
print(ln)
you've exhausted the contents of that file, and you won't be able to read them again, like you try to do later on.
Also, print does not write to a file, you want file_input.write(...)
This should fix all of that:
infile = open("lines.txt", 'r')
outfile = open("numbered_lines.txt", 'w')
line_number = 1
for line in infile:
outfile.write(str(line_number) + " " + line)
infile.close()
outfile.close()
However, here is a more pythonic way to do it:
with open("lines.txt") as infile, open("numbered_lines.txt", 'w') as outfile:
for i, line in enumerate(infile, 1):
outfile.write("{} {}".format(i, line))
Good first try, and with that, I can go through your code and explain what you did right (or wrong)
file_object=open("lines.txt",'r')
for ln in file_object:
print(ln)
This is fine, though generally you want to put a space before and after assignments (you are assigning the results of open to file_object) and add a space after a,` when separating arguments, so you might want to write that like so:
file_object = open("lines.txt", 'r')
for ln in file_object:
print(ln)
However, at this point the internal reference in the file_object have reached the end of the file, so if you wish to reuse the same object, you need to seek back to the beginning position, which is 0. As your assignment only states write to the file (and not on the screen), the above loop should be omitted from the file (but I get what you want to do, you want to see the contents of the file immediately though sometimes instructors are pretty strict on what they accept). Moving on:
count=1
file_input=open("numbered_lines.txt",'w')
for Line in file_object:
Looks pretty normal so far, again, minor formatting issues. In Python, typically we name all variables lower-case, as names with Capitalization are generally reserved for class names (if you wish to, you may read about them). Now we enter into the loop you got
print(count,' Line',(str))
This prints not quite what you want. as ' Line' is enclosed inside a quote, it is treated as a string literal - so it's treated literally as text and not code. Given that you had assigned Line, you want to take out the quotes. The (str) at the end simply just print out the string object and it definitely is not what you want. Also, you forgot to specify the file you want to print to. By default it will print to the screen, but you want to print it to the the numbered_lines.txt file which you had opened and assigned to file_input. We will correct this later.
count=+1
If you format this differently, you are assigning +1 to count. I am guessing you wanted to use the += operator to increment it. Remember this on your quiz/tests.
Finally:
file_object.close
file_input.close
They are meant to be called as functions, you need to invoke them by adding parentheses at the end with arguments, but as close takes no arguments, there will be nothing inside the parentheses. Putting everything together, the complete corrected code for your program should look like this
file_object = open("lines.txt", 'r')
count = 1
file_input = open("numbered_lines.txt", 'w')
for line in file_object:
print(count, line, file=file_input)
count += 1
file_object.close()
file_input.close()
Run the program. You will notice that there is an extra empty line between every line of text. This is because by default the print function adds a new line end character; the line you got from the file included a new-line character at the end (that's what make them lines, right?) so we don't have to add our own here. You can of course change it to an empty string. That line will look like this.
print(count, line, file=file_input, end='')
Naturally, other Python programmers will tell you that there are Pythonic ways, but you are just starting out, don't worry too much about them (although you can definitely pick up on this later and I highly encourage you to!)
The right way to open a file is using a with statement:
with open("lines.txt",'r') as file_object:
... # do something
That way, the context manager introduced by with will close your file at the end of "something " or in case of exception.
Of course, you can close the file yourself if you are not familiar with that. Not that close is a method: to call it you need parenthesis:
file_object.close()
See the chapter 7.2. Reading and Writing Files, in the official documentation.
In the first loop you're printing the contents of the input file. This means that the file contents have already been consumed when you get to the second loop. (Plus the assignment didn't ask you to print the file contents.)
In the second loop you're using print() instead of writing to a file. Try file_input.write(str(count) + " " + Line) (And file_input seems like a bad name for a file that you will be writing to.)
count=+1 sets count to +1, i.e. positive one. I think you meant count += 1 instead.
At the end of the program you're calling .close instead of .close(). The parentheses are important!

Printing as a dictionary in python

I have a problem in python. I want to create a function to print a file from user to a new file (example.txt).
The old file is like this:
{'a':1,'b':2...)
and I want the new file like:
a 1,b 2(the next line)
But the function which I made can run but it doesn't show anything in the new file. Can someone help me please.
def printing(file):
infile=open(file,'r')
outfile=open('example.txt','w')
dict={}
file=dict.values()
for key,values in file:
print key
print values
outfile.write(str(dict))
infile.close()
outfile.close()
This creates a new empty dictionary:
dict={}
dict is not a good name for a variable as it shadows the built-in type dict and could be confusing.
This makes the name file point at the values in the dictionary:
file=dict.values()
file will be empty because dict was empty.
This iterates over pairs of values in file.
for key,values in file:
As file is empty nothing will happen. However if file weren't empty, the values in it would have to be pairs of values to unpack them into key, values.
This converts dict to a string and writes it to the outfile:
outfile.write(str(dict))
Calling write with a non-str object will call str on it anway, so you could just say:
outfile.write(dict)
You don't actually do anything with infile.
You can use re module (regular expression) to achieve what you need. Solution could be like that. Of course you can customize to fit your need. Hope this helps.
import re
def printing(file):
outfile=open('example.txt','a')
with open(file,'r') as f:
for line in f:
new_string = re.sub('[^a-zA-Z0-9\n\.]', ' ', line)
outfile.write(new_string)
printing('output.txt')

Need help on sorting my file alphabetically, with a simple code

f = ("name list.txt","r")
f.sort()
for lines in f:
print(f)
This brings an error message saying
"tuple" object has no attribute f.sort()
Assuming this is python,
f = ("name list.txt","r")
is not what you want. The parentheses here create a tuple of two strings. I believe you intended to do:
f = open("name list.txt","r")
This won't fix your problem, because a file object does not have a sort method either, but it's a step in the right direction. You might be looking for lines = f.readlines(); lines.sort()
Short version
You should open the file, then use the sorted function to do a non-in-place sort, then store that to a variable:
with open("name list.txt","r") as fobj:
res = sorted(fobj)
res will be what you want, a sorted list of strings.
More detailed explanation
sorted automatically goes through each line in the file, extracts the text from that line, then sorts all the lines. This is because, in Python, files are considered iterables, you can loop over them line-by-line (like for line in fobj), and sorted accepts iterables.
with is used because it will safely close the file when everything is over, even if there is an exception. fobj = open("name list.txt","r") requires you to remember to close it afterwards, and may close if there is an exception. with takes cares of that all for you.
If you don't want newlines at the end of each line, do this:
with open("name list.txt","r") as fobj:
res = sorted(line.strip('\n') for line in fobj)
This is basically the same as above, except it goes through each line and strips the newline character before sorting.
This is the simplest way I can think of to do what you want:
with open("name list.txt", "r") as f:
for line in sorted(f.read().splitlines()):
print(line)

Python assign function to dict value

I am trying to write a script that opens files based on dictionary values. For each key/value it opens a file based on that value's name and assigns that file to the name of the value (I think it's going wrong here). So far it opens the files for me, with the right names, so that works. However, I think the name I assign the open(file) function to is wrong, since the rest of my function does not open the files anymore, and I can't close them.
Example of my script:
filelist=[]
codeconv={"agt":"r1p1d", "aga":"r2p1d"}
for value in codeconv.values():
value=open("c:\Biochemistry\Pythonscripts\Splittest\split"+value+".txt", 'w')
filelist.append(value)
"Here I do something with the files"
for f in filelist:
f.close()
So the problem is, how do I assign the open(file) function to the correct name? (Here r1p1d and r2p1d for example) And how do I later call those again?
The error I get now is:
AttributeError: 'str'object has no attribute 'close'
on the f.close() line.
EDIT: It now works as I want it to, using the following code: (I also included the 'here I do something' part now just for clarity)
result=open("C:\\Biochemistry\\Pythonscripts\\Illuminaresults.txt", "r")
filelist=[]
codeconv={"agt":"r1p1d", "aga":"r2p1d"}
opened_files={}
for key, value in codeconv.items():
filename="c:\Biochemistry\Pythonscripts\Splittest\split"+value+".txt"
file=open(filename, 'w')
opened_files.update({key: file})
for line in result:
if line[0]==">":
lastline=line
if line[0:3] in codeconv and len(line)==64:
f=opened_files[line[0:3]]
f.write(lastline+line)
else: continue
for f in opened_files.values():
f.close()
result.close()
I got another problem now though when I try to write the next part of my script, but that's probably something for another question, as it gives a Windowserror not related to this part. Thanks for the help all!
If i understand you well, you want to create some variable dynamically from the dictionary so that you can assign them to the opened files, is that it ???!!!!
I will suggest to do it using another dictionary like this:
codeconv={"agt":"r1p1d", "aga":"r2p1d"}
opened_files = {}
for key, value in codeconv.items():
file_name = "c:\Biochemistry\Pythonscripts\Splittest\split%s.txt" % value
file=open(file_name, 'w')
opened_files.update({key: file})
you can now access your opened files from the dictionary like this:
f = opened_files['agt']
f.read()
....
and for you latter code do it like this:
for f in opened_files.values():
f.close()
I'd caution against reusing "value" in that first loop, for clarity and future bugs that might be introduced there.
But, the code you posted actually works, so obviously there's something else going on that we can't diagnose here. What you need to do is inspect the contents of filelist before you try to close the file handles in it. That will probably point you towards the answer.
Perhaps you want to append tuples to filelist so the name is associated with the file object
codeconv={"agt":"r1p1d", "aga":"r2p1d"}
for value in codeconv.values():
f=open("c:\Biochemistry\Pythonscripts\Splittest\split"+value+".txt", 'w')
filelist.append((value, f))
"Here I do something with the files"
for name, f in filelist:
f.close()
I can't see a problem in your code, maybe it's not in the part pasted? (in filelist, or the snipped bit).
But anyway, I would rewrite it to avoid using the same variable, value, for two different things:
filelist = []
codeconv={"agt":"r1p1d", "aga":"r2p1d"}
for file_id in codeconv.values():
f = open("c:\Biochemistry\Pythonscripts\Splittest\split"+file_id+".txt", 'w')
filelist.append(f)
"Here I do something with the files"
for f in filelist:
if isinstance(f, str):
print "WARNING: Expected file handle, got string:", f
else:
f.close()
(I also added a bit of troubleshooting code)
The files are already open - your filelist object contains open file objects that you can iterate over (for example, with for line_of_text in filelist[0]:) or call other functions of (see dir(file) for other members).
You can defer opening the file by assigning a lambda and calling it later, for example:
for value in codeconv.values():
value2 = lambda: open(complete_filename)
filelist.append(value2)
my_file_object = filelist[0]()
You may prefer to store these in a dictionary:
for value in codeconv.itervalues():
filedict[value] = lambda: open(complete_filename)
my_file_object = filedict["r1p1d"]()
Or if you really want to create new variables (I strongly recommend not doing this, but since you asked):
for value in codeconv.itervalues():
globals()[value] = open(complete_filename)
# or
#globals()[value] = lambda: open(complete_filename)
# if you prefer
Finally, you close the files as you already are (substituting filedict.itervalues() for filelist if you use a dictionary instead).
(Obviously you need to replace complete_filename in the above examples with however you calculate the actual filename. I shouldn't need to say this, but I've been stung too often by leaving out these sorts of details.)
You're not assigning the function, you are assigning the return value. Which appearantly is a string in this case instead of a file object, which is a bit surprising. Are you doing anything with the filelist in the "here I do something" part?
[edit]
ok, sounds like you want a dict between key/fileobject (with example usage).
codeconv={"agt":"r1p1d", "aga":"r2p1d"}
filedict = {key, open("c:\Biochemistry\Pythonscripts\Splittest\split"+value+".txt", 'w')
for key, value in codeconv.iteritems()}
#Here I do something with the files
filedict["agt"].write("foo")
filedict["aga"].write("bar")
for f in filedict.values():
f.close()

Delineating a Read File

Not really too sure how to word this question, therefore if you don't particularly understand it then I can try again.
I have a file called example.txt and I'd like to import this into my Python program. Here I will do some calculations with what it contains and other things that are irrelevant.
Instead of me importing this file, going through it line-by-line and extracting the information I want.. can Python do it instead? As in, if I structure the .txt correctly (whether it be key / value pairs seperated by an equals on each line), is there a current Python 'way' where it can handle it all and I work with that?
with open("example.txt") as f:
for line in f:
key, value = line.strip().split("=")
do_something(key,value)
looks like a starting point if I understand you correctly. You need Python 2.6 or 3.x for this.
Another place to look is the csv module that can parse comma-separated value files - and you can tell it to use = as a separator instead. This will abstract away some of the "manual work" in that previous example - but it seems your example doesn't especially need that kind of abstraction.
Another idea:
with open("example.txt") as f:
d = dict([line.strip().split("=") for line in f])
Now that's concise and pythonic :)
for line in open("file")
key, value = line.strip().split("=")
key=key.strip()
value=value.strip()
do_something(key,value)
There's also another method - you can create a valid python file (let it be a list, dict definition or whatever else), read its content using
f = open('file.txt', r)
content = f.read() #assuming file isn't too long
And then just parse it:
parsedContent = eval(content)
You can pass any environment to eval (see docs), so it might not have access to your globals and locals. This is evil and wrong, but in small program that won't be distributed and won't get 'file.txt' from network or from so called malicious user - you can use it.

Categories