What is wrong with this error message? I googled for it and i still have no idea
ERROR MESSAGE
Traceback (most recent call last): File
"C:\Users\Acer\Desktop\Python Code Testing Bed\Function 6 - still in
progress.py", line 33, in
writer.writerow([id_, name, combinedaddress, dateprinter, timeprinter, ', '.join(ingredients), totalprinter.group(1)])
ValueError: I/O operation on closed file
import csv
from itertools import groupby
from operator import itemgetter
import re
with open("rofl.csv", "rb") as f, open("out.csv", "wb") as out:
reader = csv.reader(f)
next(reader)
writer = csv.writer(out)
writer.writerow(["Receipt ID","Name","Address","Date","Time","Items","Amount","Cost","Total"])
groups = groupby(csv.reader(f), key=itemgetter(0))
for k, v in groups:
v = list(v)
id_, name = v[0]
add_date_1, add_date_2 = [x[1] for x in v[1:3]]
combinedaddress = add_date_1+ " " +add_date_2
dateplustime = [ x[1] for x in v[4:8] ]
abcd = str(dateplustime)
dateprinter = re.search('(\d\d/\d\d/\d\d\d\d)\s(\d\d:\d\d)', abcd).group(1)
timeprinter = re.search('(\d\d/\d\d/\d\d\d\d)\s(\d\d:\d\d)', abcd).group(2)
transaction = [ x[1] for x in v[8:] ]
textwordfortransaction = str(transaction)
INGREDIENT_RE = re.compile(r"^\d+\s+([A-Za-z ]+)\s")
ingredients = []
for string in textwordfortransaction:
match = INGREDIENT_RE.match(string)
if match:
ingredients.append(match.groups())
continue
totalprinter = re.search(r"\bTOTAL\b\s*(\d*).",textwordfortransaction)
writer.writerow([id_, name, combinedaddress, dateprinter, timeprinter, ', '.join(ingredients), totalprinter.group(1)])
This is where you open the file:
with open("rofl.csv", "rb") as f, open("out.csv", "wb") as out:
The with block establishes the context. As soon as this context is left, the file will be closed. This:
writer.writerow([id_, name, combinedaddress, dateprinter, timeprinter, ', '.join(ingredients), totalprinter.group(1)])
…is outside the with block. The file has been closed by the time the program reaches this statement, because the with block has ended.
Indent the write.writerow to be within the with block.
Related
i stored data in a binary file.
enter code here
code 1--
import pickle as p
d = []
for i in range(3):
d1 = []
st = input("enter student name")
rl = int(input("student roll no"))
d1.append(st)
d1.append(rl)
d.extend(d1)
f = open("alex.dat", "wb")
p.dump(d,f)
f.close()
and then i printed
code 2--
import pickle as p
d = []
f = open("students.dat", "rb")
while f:
try:
d = p.load(f)
print(d)
except EOFError:
f.close()
output --
['admin', 22, 'momo', 21, 'sudhanshu', 323]
Traceback (most recent call last):
File "C:\Users\admin\AppData\Roaming\JetBrains\PyCharmCE2021.3\scratches\scratch_2.py", line 6, in
d = p.load(f)
ValueError: peek of closed file
why valueError ?
As #Maurice Mayer stated the while Condition is breaking your Code
You are writing in Code 1 everything in one file so you need just to load the file once. Checking the file-object which is already closed is breaking your Code 2
import pickle as p
d = None # Just to be sure
f = open("students.dat", "rb")
try:
d = p.load(f)
print(d)
except EOFError:
f.close()
This should work
The following code i wrote, will run one iteration with no problems. However i want it to loop through all of the values of x (which in this case there are 8). After it does the first loop through, when it goes to the second, i get an error on this line (t = f[x]['master_int'])
Traceback (most recent call last):
File "Hd5_to_KML_test.py", line 16, in <module>
t = f[x]['master_int']
TypeError: '_io.TextIOWrapper' object is not subscriptable
So it only outputs results (a .csv file and a .kml file) for BEAM0000. I was expecting it to loop through and output the two files for all 8 beams. What am I missing, why won't it loop through the other beams?
import h5py
import numpy as np
import csv
import simplekml
import argparse
parser = argparse.ArgumentParser(description='Creating a KML from an HD5 file')
parser.add_argument('HD5file', type=str)
args = parser.parse_args()
HD5file = args.HD5file
f = h5py.File(HD5file, 'r')
beamlist = []
for x in f:
t = f[x]['master_int']
for i in range(0, len(t), 1000):
time = f[x]['master_int'][i]
geolat = f[x]['geolocation']['lat_ph_bin0'][i]
geolon = f[x]['geolocation']['lon_ph_bin0'][i]
beamlist.append([time, geolat, geolon])
file = x + '.csv'
with open(file, 'w') as f:
wr = csv.writer(f)
wr.writerows(beamlist)
inputfile = csv.reader(open(file, 'r'))
kml = simplekml.Kml()
for row in inputfile:
kml.newpoint(name=row[0], coords=[(row[2], row[1])])
kml.save(file + '.kml')
When you use the context manager here:
with open(file, 'w') as f:
it reassigns to f, so when you try to access a value like f[x], it tries to call __getitem__(x) on f, which raises a TypeError
replace this block:
with open(file, 'w') as f:
wr = csv.writer(f)
wr.writerows(beamlist)
with something like:
with open(file, 'w') as fileobj:
wr = csv.writer(fileobj)
wr.writerows(beamlist)
I am working on a program that parses through log files and returns the top hits for IP addresses and a couple other things. Currently I am having trouble and I cannot interpret any of the answers to this problem to what I have going on right now. This is all of my code:
import gzip
from collections import Counter
logFileName = open('C:\\Users\\Pawlaczykm\\Desktop\\fileNames.txt', 'r')
ipAdd = []
landingPages = []
ALL_ipAdd = []
ALL_landingPages = []
# everything after this line gets done to all files
for line in logFileName.readlines():
# rstrip removes a blank line from output
# print 'Summary of: ' + line.rstrip()
# use gzip to decompress the file
with gzip.open('C:\\Users\\Pawlaczykm\\Desktop\\logFiles\\' + line.rstrip() + '.gz', 'rb') as f:
# we extract the ip addresses in lines 15-18
for eachLine in f:
parts = eachLine.split('\t')
if len(parts) > 1:
ipAdd.append(parts[2])
ALL_ipAdd.append(ipAdd)
# use gzip to decompress the file
with gzip.open('C:\\Users\\Pawlaczykm\\Desktop\\logFiles\\' + line.rstrip() + '.gz', 'rb') as f:
# we extract the landing pages
for eachLine in f:
parts = eachLine.split('\t')
if len(parts) > 1:
variable = parts[8].split('?')[0]
landingPages.append(variable)
v): (-v, k))[:10]
ALL_landingPages.append(landingPages)
ALL_ipAddDict = dict(Counter(ALL_ipAdd).most_common())
sortedALL_ipAdd = sorted(ALL_ipAddDict.iteritems(), key=lambda (k, v): (-v, k))[:10]
print 'Top IPs of all files'
print(sortedALL_ipAdd)
ALL_LandingPageDict = dict(Counter(ALL_landingPages).most_common())
sortedALL_LandingPage = sorted(ALL_LandingPageDict.iteritems(), key=lambda (k, v): (-v, k))[:10]
print 'Top landing pages of all files'
print (sortedALL_LandingPage)
Now where I am having trouble is in the following line:
ALL_ipAddDict = dict(Counter(ALL_ipAdd).most_common())
The output when I run the whole program is this:
Traceback (most recent call last):
File "C:/Users/Pawlaczykm/PycharmProjects/LogParse/parseText.py", line 35, in <module>
ALL_ipAddDict = dict(Counter(ALL_ipAdd).most_common())
File "C:\Python27\lib\collections.py", line 477, in __init__
self.update(*args, **kwds)
File "C:\Python27\lib\collections.py", line 567, in update
self[elem] = self_get(elem, 0) + 1
TypeError: unhashable type: 'list'
Can somebody help me? This is frustrating.
From your code ALL_ipAdd = [] and ipAdd = [] and ALL_ipAdd.append(ipAdd) we can conclude that ALL_ipAdd is a list of list. Counter is a subtype of dict, which hashes its items before it counts them. Lists cannot be hashed because they are mutable (if the list changed the hash would change) and thus lists can't be counted by Counter objects.
To solve this you can convert the inner lists to tuples before counting them:
ALL_ipAddDict = dict(Counter(map(tuple, ALL_ipAdd)).most_common())
That's normal. ALL_ipAdd is a list of lists. Counter needs a list, a string or any other hashable type :)
This is my CODE:
old_stdout = sys.stdout
pymol.finish_launching()
log_file=open('loops.log', "w")
sys.stdout = log_file
def myfunc(resi):
print '%s' % (resi)
file= '%s'%model_initial
x= pymol.cmd.load (file, 'r')
myspace = {'myfunc': myfunc}
d= pymol.cmd.iterate('(ss l)', 'myfunc(resi)', space=myspace)
print d
pymol.cmd.quit()
sys.stdout = old_stdout
log_file.close()
old_stdout = sys.stdout
pymol.finish_launching()
log_file=open('list.log', "w")
sys.stdout = log_file
infile = 'loops.log'
outfile = 'loops1.log'
try:
delete_list = [ "Adjusting settings to improve performance for Intel cards."]
fin = open(infile)
fout = open(outfile, "w+")
for line in fin:
for word in delete_list:
line = line.replace(word, "")
fout.write(line)
fin.close()
fout.close()
outfile = infile
except:
infile = infile
with open(INFILE) as f:
lines = f.read().splitlines()
lines = lines
lines=lines[:-1]
lines=map(int, lines) #Convert str list to int list
lines=lines[:-1]
lines=map(int, lines) #Convert str list to int list
def group_runs(li,tolerance=2):
out = []
last = li[0]
for x in li:
if x-last > tolerance:
yield out
out = []
out.append(x)
last = x
yield out
print list(group_runs(lines))
sys.stdout = old_stdout
log_file.close()
old_stdout = sys.stdout
pymol.finish_launching()
log_file=open('list.log', "w")
sys.stdout = log_file
fhand=open('list.log')
for lines in fhand:
print lines
for k, g in groupby(enumerate(lines), lambda (i,x):i-x):
ranges = []
group = map(itemgetter(1), g)
ranges.append((group[0], group[-1]))
print ranges
sys.stdout = old_stdout
log_file.close()
the file loops.log, is a file with many numbers (select them of model initial, and the function principal of this CODE is group them for example:
1
2
3
4
8
9
11
15
group them:
(1,4)
(8,9)
(11, 11)
(15, 15)
This CODE works, but, when file have many many numbers, I have this message :
Traceback (most recent call last):
File "./automaticvF.py", line 232, in <module>
lines=map(int, lines) #Convert str list to int list
ValueError: invalid literal for int() with base 10: 'Adjusting settings to improve performance for Intel cards.
and the log is white. I Think that is more easy remove the message of my file loops.log, and change name of file. this only happens when have many many many numbers to group, but when I have 80 numbers for example, no problem. so What can I change in this CODe, for use the variable infile, depending if the message is or not is in the infile old?
Can somebody help me please?
Thanks
Instead of redirecting the standard output of your process to a file by changing sys.stdout, you need to write your data to the file directly. You can do it, for example, like this:
print >> log_file, '%s' % (resi)
Or like this:
log_file.write(str(resi))
This will ensure that your file will not contain random unrelated data that happens to be printed to the standard output.
I have a list, abbreviations, filled with string objects. I am trying to call the .index of a string in my list. When I call the .index method with a string I get a ValueError: 'LING' is not in list, when it clearly is in the list.
My code:
for item in abbreviations:
print item
print abbreviations.index("LING")
Why does 'LING' not exist when it clearing does? I have added my following lines of code, which searches 'abbreviations' for the index of a string. I am baffled -- "LING" is clearly in my abbreviations list.
EDIT (Additional Code):
import csv
myfile = open("/Users/it/Desktop/Classbook/classAbrevs.csv", "rU")
lines = [tuple(row) for row in csv.reader(myfile)]
longSubjectNames = []
abbreviations = []
masterAbrevs = []
for item in lines:
longSubjectNames.append(item[0])
abbreviations.append(item[1])
with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as myfile:
masterSchedule = tuple(open("/Users/it/Desktop/Classbook/masterClassList.txt", 'r'))
for masterline in masterSchedule:
masterline.strip()
masterSplitLine = masterline.split("|")
subjectAbrev = ""
if masterSplitLine[0] != "STATUS":
subjectAbrev = ''.join([i for i in masterSplitLine[2] if not i.isdigit()])
masterAbrevs.append(subjectAbrev)
finalAbrevs = []
for subject in masterAbrevs:
if (subject[-1] == 'W') and (subject[-2:] != 'UW'):
subject = subject[:-1]
finalAbrevs.append(subject)
x = 0
for item in abbreviations:
print item
print abbreviations.index("LING")
for item in finalAbrevs:
if masterSplitLine[0] != "STATUS":
concat = abbreviations.index(str(finalAbrevs[x]).strip())
print "The abbreviation for " + str(item) + " is: " + longSubjectNames[concat]
x = x + 1
The output of:
masterAbrevs = []
for item in lines:
longSubjectNames.append(item[0])
abbreviations.append(item[1])
print '-'.join(abbreviations)
is:
ACA-ACCY-AFST-AMST-ANAT-ANTH-APSC-ARAB-AH-FA-ASTR-BIOC-BISC-BME-BMSC-BIOS-BADM-CHEM-CHIN-CE-CLAS-CCAS-COMM-CSCI-CFA-CNSL-CPED-DNSC-EALL-ECON-EDUC-ECE-EHS-ENGL-EAP-EMSE-ENRP-EPID-EXSC-FILM-FINA-FORS-FREN-GEOG-GEOL-GER-GREK-HCS-HSCI-HLWL-HSML-HEBR-HIST-HOMP-HONR-HDEV-HOL-HSSJ-ISTM-IDIS-IAD-INTD-IAFF-IBUS-ITAL-JAPN-JSTD-KOR-LATN-LAW-LSPA-LING -MGT-MKTG-MBAD-MATH-MAE-MED-MICR-MMED-MSTD-MUS-NSC-ORSC-PSTD-PERS-PHAR-PHIL-PT-PA-PHYS-PMGT-PPSY-PSC-PORT-PSMB-PSYD-PSYC-PUBH-PPPA-REL-SEAS-SMPA-SLAV-SOC-SPAN-SPED-SPHR-STAT-SMPP-SUST-TRDA-TSTD-TURK-UW-WLP-WSTU
Traceback (most recent call last):
File "/Users/it/Desktop/Classbook/sortClasses.py", line 25, in <module>
with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as anything:
IOError: [Errno 2] No such file or directory: '/Users/it/Desktop/Classbook/masterClassList.txt'
myfile = open("/Users/it/Desktop/Classbook/classAbrevs.csv", "rU")
lines = [tuple(row) for row in csv.reader(myfile)]
longSubjectNames = []
abbreviations = []
masterAbrevs = []
for item in lines:
longSubjectNames.append(item[0])
abbreviations.append(item[1])
with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as myfile:
The problem is here;
with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as myfile:
You defined myfile before here,
myfile = open("/Users/it/Desktop/Classbook/classAbrevs.csv", "rU")
So actually abbreviations = [] is not taking data from classAbrevs.csv.Because it's taking data from masterClassList.txt as you defined myfile with this line;
with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as myfile
That's why your string not in that list.Also this line;
for item in lines:
longSubjectNames.append(item[0])
abbreviations.append(item[1])
Are you sure is item[1] has all of the strings that you want?
And I tried these codes I just copy-pasted it from your's and here is the result;
The problem is, from the result you ran:
"LING\t" is shown in your list, not "LING"
with running this I get the desired index:
abbreviations.index("LING\t")
71
To correct this, there are many methods to strip the \t, I'm showing one of those:
abbreviations.append(item[1].strip())
By correcting this line, your item[1] will strip the \t before appending to your abbreviations list.