I currently have this code that successfully reads information from two sources and correctly formats them into an output file \_spec_final.t15. Currently the information prints one after another, but I would like it to print the information for one line/file then overwrite it with the next iteration. Does anybody know how to do this?
with open('\\_spec_final.t15', 'w') as f:
with open('info.txt', 'rt') as infofile:
for count, line in enumerate(infofile):
print count
lat = float(line[88:94])
lon = float(line[119:127])
year = int(line[190:194])
month = int(line[195:197])
day = int(line[198:200])
hour = int(line[201:203])
minute = int(line[204:206])
second = int(line[207:209])
dur = float(line[302:315])
numpoints = float(line[655:660])
fov = line[481:497] # field of view?
sza = float(line[418:426])
snr = 0.0000
roe = 6396.2
res = 0.5000
lowwav = float(lowwav)
highwav = float(highwav)
spacebw = (highwav - lowwav)/ numpoints
d = datetime.datetime(year, month, day, hour, minute, second)
f.write('{:>12.5f}{:>12.5f}{:>12.5f}{:>12.5f}{:>8.1f}'.format(sza,roe,lat,lon,snr)) # line 1
f.write("\n")
f.write('{:>10d}{:>5d}{:>5d}{:>5d}{:>5d}{:>5d}'.format(year,month,day,hour,minute,second)) # line 2
f.write("\n")
f.write( ('{:%Y/%m/%d %H:%M:%S}'.format(d)) + "UT Solar Azimuth:" + ('{:>6.3f}'.format(sza)) + " Resolution:" + ('{:>6.4f}'.format(res)) + " Duration:" + ('{:>6.2f}'.format(dur))) # line 3
f.write("\n")
f.write('{:>21.13f}{:>26.13f}{:>24.17e}{:>12f}'.format(lowwav,highwav,spacebw,numpoints)) # line 4
f.write("\n")
with open(files[count], 'r') as g:
for line in g:
wave_no, intensity = [float(item) for item in line.split()]
if lowwav <= wave_no <= highwav:
f.write(str(intensity) + '\n')
Open and write to the file after you read in infofile.
It will open and overwrite \_spec_final.t15 with each iteration.
with open('info.txt', 'rt') as infofile:
for count, line in enumerate(infofile):
print count
with open('\\_spec_final.t15', 'w') as f:
Related
This is the output I need:
Temperature anomaly filename:SacramentoTemps.csv
Enter window size:60
1940,-0.2331
1941,-0.2169
1942,-0.2150
1943,-0.2228
1944,-0.2107
1945,-0.1796
1946,-0.1667
1947,-0.1582
1948,-0.1585
1949,-0.1492
1950,-0.1711
1951,-0.1688
1952,-0.1490
1953,-0.1556
1954,-0.1548
1955,-0.1580
1956,-0.1420
1957,-0.1101
1958,-0.1017
This is my code:
filename = input("Temperature anomaly filename:")
infile = open(filename, "r")
k = int(input("Enter window size:"))
infile.readline()
temp_list = []
for line in infile:
line = line.strip()
year,temp = line.split(",")
temp = float(temp)
temp_list.append(temp)
index = k
for index in range(index,len(temp_list)-1-index):
year = 1880 + index
ave = sum(temp_list[index:index+k]) / (2*index+1)
print(str(year)+","+"{:.4f}".format(ave))
infile.close()
My code currently prints out up until the year 1957 and it prints out the wrong averages for each year. What do I need to fix?
filename = "SacramentoTemps.csv"
infile = open(filename, "r")
k = int(input("Enter window size:"))
temp_list = []
for line in infile:
line = line.strip()
year, temp = line.split(",")
temp = float(temp)
temp_list.append(temp)
infile.close()
moving_average = []
for i, temp in enumerate(temp_list):
average = temp
if len(temp_list) - i < k:
break
for j in range(k):
average += temp_list[i+j]
moving_average.append(average/k)
print(str(year) + "," + "{:.4f}".format(average))
I coded in the direction of modifying your code as little as possible.
One thing to note is your file need to be longer than window size.
Using pandas would be most sane way to go:
import pandas as pd
filename = "SacramentoTemps.csv"
window = 2
data = pd.read_csv(filename)
data.temperature.rolling(window = window).mean().fillna(data.temperature)
My Python code for data saves data of a temperature sensor(DS18B20) but doesn't save the Temperature in a column. The Temperature is written in a line like that: ['25.25']. Can somebody please tell me how to fix it. I have to separate the read Temperature from the Sensor into several Numbers. Thanks for your help
while programmStatus == 1:
#while True:
now = datetime.now(timezoneBerlin)
#while True:
# open new file
with open ( 'test.tsv', 'w') as f:
for temperature_single in Temperature:
f.write ("{:>5}\n".format('Temperature'))
f.write("\t{:>5}\n".format
(format(temperature_single)))
f.write("\n")
f.flush()
f.close()
x = 0
ds1820readout()
print ("Sensorname und Temperaturevalue:")
while x < tempSensorQuantity:
print (tempSensorName[x] , " " , tempSensorValue[x], " °C")
x = x + 1
print ("\n")
In this code, there is an I/0 error on closed file, can somebody please help?
It seems that Temperature is a list of values. If you try to put them into the csv you have to iterate over the list first, e.g.:
tempSensorName = []
tempSensorQuantity = 0
tempSensorValue = []
programmStatus = 1
Temperature = tempSensorValue
def ds1820einlesen():
global tempSensorName, tempSensorQuantity, programmStatus
def ds1820auslesen():
global tempSensorName, tempSensorQuantity, tempSensorValue,
with open ( 'test.tsv', 'w') as f:
for temperature_single in Temperature:
f.write ("{:>5}\n".format('Temperature'))
f.write("\t{:>5}\n".format(format(temperature_single)))
f.write("\n")
f.flush()
f.close()
x = 0
ds1820auslesen()
print ("Sensorname and Temperaturevalue:")
while x < tempSensorQuantity:
print (tempSensorName[x] , " " ,
tempSensorValue[x], " °C")
x = x + 1
print ("\n")
I know that there are some topics on this that tell us to use .strip() or .rstrip() function to do this but it's not working for me.
I have a programme that appends a new line to the csv file but unfortunately it generates a trailing comma...
I have tried to remove it with the .strip() function in python but it isn't working well, I am doing something wrong?
This is an example of what happen when I input 'T123' for Bike_No and '05/08/2017' for Purchase_Date
from datetime import datetime
td= datetime.now()
initial_bike_detaillist=[]
deBatt =100
deKM = 0.00
deDate = str(td)[8:10] + "/"+ str(td)[5:7] + "/"+ str(td)[0:4]
print("Option 4: Add abicycle \n")
Bike_No=input("Bike No. :")
Purchase_Date=str(input("Purchase Date:"))
initial_bike_detaillist=[str(Bike_No),str(Purchase_Date),str(deBatt),str(deDate),str(deKM)]#because there is no write function for int
filename="Assignment_Data1.csv"
file=open(filepath + filename,"a")
file.write("\n")
for k in initial_bike_detaillist:
file.write("{},".format(k))
print("Bicycle ({}) has been created".format(Bike_No))
file.close()
file=open(filepath + filename,"r")
for line in file:
line.strip()
print(line)
expected output=
Bike No.,Purchase Date,Batt %,Last Maintenance,KM since Last
T101,10/04/2016,55,10/01/2017,25.08
T102,01/07/2016,10,15/05/2017,30.94
T103,15/11/2016,94,13/06/2017,83.16
T104,25/04/2017,58,10/01/2017,25.08
T105,24/05/2017,5,20/06/2017,93.80
T123,04/04/2017,100,05/08/2017,0.0
actual output:
Bike No.,Purchase Date,Batt %,Last Maintenance,KM since Last
T101,10/04/2016,55,10/01/2017,25.08
T102,01/07/2016,10,15/05/2017,30.94
T103,15/11/2016,94,13/06/2017,83.16
T104,25/04/2017,58,10/01/2017,25.08
T105,24/05/2017,5,20/06/2017,93.80
T123,04/04/2017,100,05/08/2017,0.0,
`
Instead of this line :
for k in initial_bike_detaillist:
file.write("{},".format(k))
use following line :
file.write(','.join(initial_bike_detaillist))
Your Code :
from datetime import datetime
td = datetime.now()
initial_bike_detaillist = []
deBatt = 100
deKM = 0.00
deDate = str(td)[8:10] + "/" + str(td)[5:7] + "/" + str(td)[0:4]
print("Option 4: Add abicycle \n")
Bike_No = input("Bike No. :")
Purchase_Date = str(input("Purchase Date:"))
initial_bike_detaillist = [str(Bike_No), str(Purchase_Date), str(deBatt), str(deDate),
str(deKM)] # because there is no write function for int
filename = "Assignment_Data1.csv"
file = open(filepath + filename, "a")
file.write("\n")
# for k in initial_bike_detaillist:
# file.write("{},".format(k))
file.write(','.join(initial_bike_detaillist)) # use this line .
print("Bicycle ({}) has been created".format(Bike_No))
file.close()
file = open(filepath + filename, "r")
for line in file:
# line.strip() # Then, not need this line
print(line)
I can use with open in Python to iterate through a Comma Sep text file but I wish to add another level of complexity.
I need to return the value at index[0] of a row where a string is found.
For example, I have a text file containing:
00:00,19.90,990.49,59.16,11.78,No
01:00,19.92.991.00,59.75,11.90,Yes
02:00,19.76,991.21,58.87,10.95,No
03:00,19.34,989.97,57.00,10.64,Yes
now I use:
MaxTemp = -float('inf')
MinTemp = +float('inf')
with open (YdPath + yFileDate + '.txt', 'r') as DailyData:
for lines in DailyData:
temp = float(lines.strip().split(',')[1])
if MaxTemp < temp:
MaxTemp = temp
if MinTemp > temp:
MinTemp = temp
and output would be:
MaxTemp = 19.92
MinTemp = 19.34
now though I wish to get the index[0] value that relates to these entries, i.e.
MaxTemp needs to find that the entry 19.92 in in the row starting with index[0] of 01:00 & be displayed as such, using variable tTime as index[0] value:
print 'The Max Temp was ' + MaxTemp + ' recorded at ' + tTime
Thanks for looking
UPDATE
thanks goes to Henry Heath for the help & pointers.
Needed 2 time variables as the MaxTemp & MinTemp were returning the exact same time using tTime here is the correct working code:
MaxTemp = -float('inf')
MinTemp = +float('inf')
with open (YdPath + yFileDate + '.txt', 'r') as DailyData:
for line in DailyData:
line = line.strip().split(',')
temp = float(line[1])
if MaxTemp < temp:
MaxTemp = temp
MXtTime = line[0]
if MinTemp > temp:
MinTemp = temp
MNtTime = line[0]
MaxTemps = '%.1f' %MaxTemp
MinTemps = '%.1f' %MinTemp
print('The Max Temp was ' + MaxTemps + ' recorded at ' + MXtTime)
print('The Min Temp was ' + MinTemps + ' recorded at ' + MNtTime)
This might be easier if you use the csv library
MaxTemp = -float('inf')
MinTemp = +float('inf')
with open (YdPath + yFileDate + '.txt', 'r') as DailyData:
for line in DailyData:
line = line.strip().split(',')
temp = float(line[1])
if MaxTemp < temp:
MaxTemp = temp
tTime = line[0]
if MinTemp > temp:
MinTemp = temp
Following up on #henry-heath ’s suggestion:
import csv
from decimal import Decimal
# n.b., this implementation uses a sort for the brevity of
# implementation. However, the original iterative approach
# will be much faster (O(n) compared to O(n log n)) than
# the version below
with open (YdPath + yFileDate + '.txt', 'r') as f:
reader = csv.reader(f)
lines = [line for line in reader]
lines.sort(key=lambda x: Decimal(x[1]))
print 'The Max Temp was ' + lines[-1][1] + ' recorded at ' + lines[-1][0]
print 'The Min Temp was ' + lines[0][1] + ' recorded at ' + lines[0][0]
Iterative version:
import csv
from decimal import Decimal
with open (YdPath + yFileDate + '.txt', 'r') as f:
reader = csv.reader(f)
line = reader.next()
line[1] = Decimal(line[1])
min_temp, max_temp = line, line
for x in reader:
x[1] = Decimal(x[1])
if x[1] < min_temp[1]: min_temp = x
if x[1] > max_temp[1]: max_temp = x
print 'The Max Temp was ' + max_temp[1] + ' recorded at ' + max_temp[0]
print 'The Min Temp was ' + min_temp[1] + ' recorded at ' + min_temp[0]
I am doing text processing and using 'readline()' function as follows:
ifd = open(...)
for line in ifd:
while (condition)
do something...
line = ifd.readline()
condition = ....
#Here when the condition becomes false I need to rewind the pointer so that the 'for' loop read the same line again.
ifd.fseek() followed by readline is giving me a '\n' character. How to rewind the pointer so that the whole line is read again.
>>> ifd.seek(-1,1)
>>> line = ifd.readline()
>>> line
'\n'
Here is my code
labtestnames = sorted(tmp)
#Now read each line in the inFile and write into outFile
ifd = open(inFile, "r")
ofd = open(outFile, "w")
#read the header
header = ifd.readline() #Do nothing with this line. Skip
#Write header into the output file
nl = "mrn\tspecimen_id\tlab_number\tlogin_dt\tfluid"
offset = len(nl.split("\t"))
nl = nl + "\t" + "\t".join(labtestnames)
ofd.write(nl+"\n")
lenFields = len(nl.split("\t"))
print "Reading the input file and converting into modified file for further processing (correlation analysis etc..)"
prevTup = (0,0,0)
rowComplete = 0
k=0
for line in ifd:
k=k+1
if (k==200): break
items = line.rstrip("\n").split("\t")
if((items[0] =='')):
continue
newline= list('' for i in range(lenFields))
newline[0],newline[1],newline[3],newline[2],newline[4] = items[0], items[1], items[3], items[2], items[4]
ltests = []
ltvals = []
while(cmp(prevTup, (items[0], items[1], items[3])) == 0): # If the same mrn, lab_number and specimen_id then fill the same row. else create a new row.
ltests.append(items[6])
ltvals.append(items[7])
pos = ifd.tell()
line = ifd.readline()
prevTup = (items[0], items[1], items[3])
items = line.rstrip("\n").split("\t")
rowComplete = 1
if (rowComplete == 1): #If the row is completed, prepare newline and write into outfile
indices = [labtestnames.index(x) for x in ltests]
j=0
ifd.seek(pos)
for i in indices:
newline[i+offset] = ltvals[j]
j=j+1
if (rowComplete == 0): #
currTup = (items[0], items[1], items[3])
ltests = items[6]
ltvals = items[7]
pos = ifd.tell()
line = ifd.readline()
items = line.rstrip("\n").split("\t")
newTup = (items[0], items[1], items[3])
if(cmp(currTup, newTup) == 0):
prevTup = currTup
ifd.seek(pos)
continue
else:
indices = labtestnames.index(ltests)
newline[indices+offset] = ltvals
ofd.write(newline+"\n")
The problem can be handled more simply using itertools.groupby. groupby can cluster all the contiguous lines that deal with the same mrn, specimen_id, and lab_num.
The code that does this is
for key, group in IT.groupby(reader, key = mykey):
where reader iterates over the lines of the input file, and mykey is defined by
def mykey(row):
return (row['mrn'], row['specimen_id'], row['lab_num'])
Each row from reader is passed to mykey, and all rows with the same key are clustered together in the same group.
While we're at it, we might as well use the csv module to read each line into a dict (which I call row). This frees us from having to deal with low-level string manipulation like line.rstrip("\n").split("\t") and instead of referring to columns by index numbers (e.g. row[3]) we can write code that speaks in higher-level terms such as row['lab_num'].
import itertools as IT
import csv
inFile = 'curious.dat'
outFile = 'curious.out'
def mykey(row):
return (row['mrn'], row['specimen_id'], row['lab_num'])
fieldnames = 'mrn specimen_id date lab_num Bilirubin Lipase Calcium Magnesium Phosphate'.split()
with open(inFile, 'rb') as ifd:
reader = csv.DictReader(ifd, delimiter = '\t')
with open(outFile, 'wb') as ofd:
writer = csv.DictWriter(
ofd, fieldnames, delimiter = '\t', lineterminator = '\n', )
writer.writeheader()
for key, group in IT.groupby(reader, key = mykey):
new = {}
row = next(group)
for key in ('mrn', 'specimen_id', 'date', 'lab_num'):
new[key] = row[key]
new[row['labtest']] = row['result_val']
for row in group:
new[row['labtest']] = row['result_val']
writer.writerow(new)
yields
mrn specimen_id date lab_num Bilirubin Lipase Calcium Magnesium Phosphate
4419529 1614487 26.2675 5802791G 0.1
3319529 1614487 26.2675 5802791G 0.3 153 8.1 2.1 4
5713871 682571 56.0779 9732266E 4.1
This seems to be a perfect use case for yield expressions. Consider the following example that prints lines from a file, repeating some of them at random:
def buflines(fp):
r = None
while True:
r = yield r or next(fp)
if r:
yield None
from random import randint
with open('filename') as fp:
buf = buflines(fp)
for line in buf:
print line
if randint(1, 100) > 80:
print 'ONCE AGAIN::'
buf.send(line)
Basically, if you want to process an item once again, you send it back to the generator. On the next iteration you will be reading the same item once again.