How to append a string from dictionary? - python

X = corpus.get("Andrew Shapiro")
testsite_array = []
with X as my_file:
for line in my_file:
testsite_array.append(line)
where corpus is a dictonary and Andrew Shapiro is an item in it. It gives me following error.
File "C:/Users/Vrushab PC/Downloads/Dissertation/untitled0.py", line 71, in <module>
with X as my_file:
AttributeError: __enter__

In order to use the with statement, the object, X in this case, the object must have implemented the enter method and exit method. The whole point is that it allow for the object to clean it's self up even in the case of an exception. Think try:except:finally done much more cleanly.
In order to answer your question though, I would need to know what you expected X to be. You named your temporary placeholder for it as my_file so is X supposed to be a file path that you want to open or something?
A full example of what you are attempting to do would be helpful.
Generally though, you would use the with statement for doing things like opening files like this:
with open(X, 'r') as my_file:
...
Tahoe

Related

Why is my CSV File not being printed?

My code currently writes a dictionary which contains scores for a class to a CSV file. This part is correctly done by the program and the scores are wrote to file, however the latest dictionary written to file is not printed. For example, after the code has been ran once, it will not be printed however once the code has been ran for a second time, the first bit of data is printed however the new data isn't. Can someone tell me where I am going wrong?
SortedScores = sorted(Class10x1.items(), key = lambda t: t[0], reverse = True) #this sorts the scores in alphabetical order and by the highest score
FileWriter = csv.writer(open('10x1 Class Score.csv', 'a+'))
FileWriter.writerow(SortedScores) #the sorted scores are written to file
print "Okay here are your scores!\n"
I am guessing the problem is here somewhere however I cannot quite pinpoint what or where it is. I have tried to solve this by changing the mode of the file when it is read back in to r, r+ and rb, however all have the same consequence.
ReadFile = csv.reader(open("10x1 Class Score.csv", "r")) #this opens the file using csv.reader in read mode
for row in ReadFile:
print row
return
from Input output- python docs:
It is good practice to use the with keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks:
>>> with open('workfile', 'r') as f:
... read_data = f.read()
>>> f.closed
True
File objects have some additional methods, such as isatty() and truncate() which are less frequently used; consult the Library Reference for a complete guide to file objects.
I'm not sure why they bury that so far in the documentation since it is really useful and a very common beginner mistake:
SortedScores = sorted(Class10x1.items(), key = lambda t: t[0], reverse = True) #this sorts the scores in alphabetical order and by the highest score
with open('10x1 Class Score.csv', 'a+') as file:
FileWriter = csv.writer(file)
FileWriter.writerow(SortedScores) #the sorted scores are written to file
print "Okay here are your scores!\n"
this will close the file for you even if an error is raised which prevents many possibilities of loss of data
the reason it did not appear to write to the file is because when you do .write_row() it doesn't immediately write to the hard drive, only to a buffer which is occasionally emptied into the file on hard drive, although with only one write statement it has no need to empty.
Remember to close the file after operation, otherwise the data will not be saved properly.
Try to use with keyword so that Python will handle the closure for you:
import csv
with open('10x1 Class Score.csv', 'a+') as f:
csv_writer = csv.writer(f)
# write something into the file
...
# when the above block is done, file will be automatically closed
# so that the file is saved properly

Get line number python from opened file

I wrote this little Python 2.7 prototype script to try and read specified lines (in this example lines 3,4,5) from a formatted input file. I am going to be later parsing data from this and operating on the input to construct other files.
from sys import argv
def comparator (term, inputlist):
for i in inputlist:
if (term==i):
return True
print "fail"
return False
readthese = [3,4,5]
for filename in argv[1:]:
with open(filename) as file:
for line in file:
linenum=#some kind of way to get line number from file
if comparator(linenum, readthese):
print(line)
I fixed all the errors I had found with the script but currently I don't see anyway to get a line number from file. It's a bit different than pulling the line number from a file object since file is a class not an object if I'm not mistakened. Is there someway I can pull the the line number for my input file?
I think a lot of my confusion probably stems from what I did with my with statement so if someone could also explain what exactly I have done with that line that would be great.
You could just enumerate the file object since enumerate works with anything iterable...
for line_number, line in enumerate(file):
if comparator(line_number, line):
print line
Note, this indexes starting at 0 -- If you want the first line to be 1, just tell enumerate that's where you want to start:
for line_number, line in enumerate(file, 1):
...
Note, I'd recommend not using the name file -- On python2.x, file is a type so you're effectively shadowing a builtin (albeit a rarely used one...).
You could also use the list structure's index itself like so:
with open('a_file.txt','r') as f:
lines = f.readlines()
readthese = [3,4,5]
for lineno in readthese:
print(lines[1+lineno])
Since the list of lines already implicitly contains the line numbers based on index+1
If the file is too large to hold in memory you could also use:
readthese = [3,4,5]
f = open('a_file.txt','r')
for lineno in readthese:
print(f.readline(lineno+1))
f.close()

Python using open(file) variable more than once?

Is it possible to define a variable as open("file.txt", "a") and call it more than once so you don't have to keep typing open("file.txt", "a")?
I tried, but it doesn't seem to work for me. I keep getting the error message:
ValueError: I/O operation on closed file.
My code looks like:
x = open("test.txt", "a")
with x as xfile:
xfile.write("hi")
#works fine until I try again later in the script
with x as yfile:
yfile.write("hello")
Question: Is there a way to do this that I'm missing?
(My apologies if this question is a repeat, I did search google and SO before I posted a new question.)
If you don't want to close the file right away, don't use a with statement and close it yourself when you're done.
outfile = open('test.txt', 'w')
outfile.write('hi\n')
x = 1
y = 2
z = x + y
outfile.write('hello\n')
outfile.close()
Typically you use the with statement when you want to open a file and do something with that file immediately, and then close it.
with open('test.txt', 'w') as xfile:
do something with xfile
However, it is best practice to take care of all your I/O with a single file at once if you can. So if you want to write several things to a file, put those things into a list, then write the contents of the list.
output = []
x = 1
y = 2
z = x + y
output.append(z)
a = 3
b = 4
c = a + b
output.append(c)
with open('output.txt', 'w') as outfile:
for item in output:
outfile.write(str(item) + '\n')
The with statement closes the file automatically. It is good to do everything related to the file inside the with statement (opening a file multiple times is also not a good idea).
with open("test.txt", "a") as xfile:
# do everything related to xfile here
But, if it doesn't solve your problem then don't use the with statement and close the file manually when the work related to that file is done.
From docs:
It is good practice to use the with keyword when dealing with file
objects. This has the advantage that the file is properly closed after
its suite finishes, even if an exception is raised on the way.
You example doesn't make much sense without more context, but I am going to assume that you have a legitimate need to open the same file multiple times. Answering exactly what you are asking for, you can try this:
x = lambda:open("test.txt", "a")
with x() as xfile:
xfile.write("hi")
#works fine until I try again later in the script
with x() as yfile:
yfile.write("hello")
The usual way to use with statements (and indeed I think the only way they work) is with open("text.txt") as file:.
with statements are used for handling resources that need to be opened and closed.
with something as f:
# do stuff with f
# now you can no longer use f
is equivalent to:
f = something
try:
# do stuff with f
finally: # even if an exception occurs
# close f (call its __exit__ method)
# now you can no longer use f

How to get number of objects in a pickle?

Is there a short way to get number of objects in pickled file - shorter than writing a function that opens the file, keeps calling pickle.load method and updating num_of_objs by 1 until it catches EOFError and returns the value?
No, there isn't. The pickle format does not store that information.
If you need that type of metadata, you need to add it to the file yourself when writing:
pickle.dump(len(objects), fileobj)
for ob in objects:
pickle.dump(ob, fileobj)
Now the first record tells you how many more are to follow.
There is no direct way of finding the length of a pickle, but if you are afraid of running an endless loop you could try the following,
company_id_processed=[]
with open("responses_pickle.pickle", "rb") as f:
while True:
try:
current_id=pickle.load(f)['name']
company_id_processed.append(current_id)
except EOFError:
print 'Pickle ends'
break
The best way is to store and load data object with descriptive file name. For example, if you want to save two dataframes, you can name the pickle file as "datasets_name_2DFs.pickle". When you want to load them, you can simply get the number in the file name and use for loop equals to that number to get the pickle object. This is easier for me. For the code part, you can do what ever suits you.
Or you can use other methods like this:
with open(path, "wb") as f:
pickle.dump(len(data), f)
for value in data:
pickle.dump(value, f)
data_list = []
with open(path, "rb") as f:
for _ in range(pickle.load(f)):
data_list.append(pickle.load(f))
print data_list

Setting properties from CSV file using eval (Python)

I have CSV files that contain numerous values that I want to reference. I wanted to parse them succinctly using eval. Here's what I tried:
line = fileHandle.readline()
while line != "":
if line != "\n":
parameter = line.split(',')[0]
value = line.split(',')[2].replace("\n", "")
eval("%s = \"%s\"" % (parameter, value))
print(parameter + " = " + eval(parameter)) # a quick test
line = fileHandle.readline()
What I get is:
Traceback (innermost last):
File "<string>", line 73, in ?
File "<string>", line 70, in createJMSProviders
File "<string>", line 49, in createJMSProviderFromFile
File "<string>", line 1
externalProviderURL="tibjmsnaming://..."
^
SyntaxError: invalid syntax
I reads to me like it is not possible to eval("externalProviderURL=\"tibjmsnaming://...\""). What is wrong with this statement?
As per S.Lott's suggestion, here is how I would solve this issue. I might be simplifying a little bit. If so, I apologize, but I haven't seen your data.
import csv
my_dict = {}
with open('my/data.csv') as f:
my_reader = csv.reader(f)
for row in my_reader:
my_dict[row[0]] = row[2]
As you can see, there are a number of differences from your code here. First of all, I'm using Python's with statement, which is a good habit to get into when working with files. Second, I'm using python's csv module to create a reader object, which you can iterate over in a for loop. This is significantly more pythonic than using a while loop. Finally, and probably most relevantly, I'm adding these values to a dictionary, rather than trying to plop them into variables in the global scope. To access these parameters, you can simply do the following:
my_dict['externalProviderURL']
However, you get a lot more than this. Storing your values in an actual data structure will allow you use all of it's built in methods. For example, you can go back and iterate over it's keys and values
for key, value in my_dict.iteritems():
print key
print value
Pythonic code often involves a significant use of dictionaries. They're finely tuned for performance, and are made particularly useful since most anything can be stored as a value in the dictionary (lists, other dictionaries, functions, classes etc.)
eval() is for evaluation Python expressions, and assignment (a = 1) is a statement.
You'll want exec().
>>> exec("externalProviderURL=\"tibjmsnaming://...\"")
>>> externalProviderURL
'tibjmsnaming://...'
(FYI, to use eval() you'd have to do externalProviderURL=eval("\"tibjmsnaming://...\""), but it looks like your situation is more suited to exec).

Categories

Resources