I am having a hard time figuring out how to read multiple lines of a text file and produce the required output. I tried the datetime format but it is not going anywhere. I would greatly appreciate any help
What is being asked is:
Write Photo Report - For this part of the project we will take a data file and use Control Break logic on it to produce a report to the screen. The control break will be on the Year the file was created. The data file will have the following format (one field per line):
Date Created (in the form DD-MM-YYYY) Filename
Number of Bytes
For example, the following input file:
25-02-2019
MyTurtle.GIF
6000
11-05-2019
Smokey.GIF
4000
I am not able read and output the date in the file. What I currently have is:
def openFile(self):
myFile = self.inputFile.getText()
fileName = open(myFile, "r")
text = fileName.readline()
x = "%4s%25s%25s\n\n" % ("File Name", "Date Created", "Number of Bytes")
date_str_format = '%Y-%m-%d'
jobs = []
for i in fileName:
d = datetime.strptime(i[0],'%m-%d-%Y')
if d in i:
date = i
x += "%4d\n" % date
You can use the built-in module re to extract all the file names, dates and bytes from your file:
import re
from datetime import datetime
with open('file.txt', 'r') as f:
t = f.read()
dates = re.findall('\d\d-\d\d-\d\d\d\d', t) # Find all the dates in the form of 00-00-0000
files = re.findall('\w+\.\w+', t) # Find all the file names in the form of text.text
byte = re.findall('(?<!-)\d\d\d\d', t) # Find all the number of bytes in the for of a four digit number without a dash behind it
print("File Name".ljust(15), "Date Created".ljust(15), "Number of Bytes".ljust(15))
for d, f, b in zip(dates, files, byte):
print(f.ljust(15), d.ljust(15), b.ljust(15))
Output:
File Name Date Created Number of Bytes
MyTurtle.GIF 25-02-2019 6000
Smokey.GIF 11-05-2019 4000
For this, you need to read all the lines in the file then process 3 lines at a time to generate a data row for the report.
Try this code:
from datetime import datetime
ss = '''
25-02-2019
MyTurtle.GIF
6000
11-05-2019
Smokey.GIF
4000
'''.strip()
with open ('myfile.txt','w') as f: f.write(ss) # write data file
#############################
def openFile(self):
fileName = open('myFile.txt', "r")
x = "{:<20}{:<25}{:<25}".format("File Name", "Date Created", "Number of Bytes")
print(x) # header row
lines = fileName.readlines() # all lines in file
for i in range(0,len(lines),3): # index every 3 lines
dt = lines[i].strip() # date
filename = lines[i+1].strip() # file name
filesize = lines[i+2].strip() # file size
d = datetime.strptime(dt,'%d-%m-%Y') # format date
x = "{:<20}{:<25}{:<25}".format(filename, str(d), str(filesize)) # data row
print(x)
openFile(None)
Output
File Name Date Created Number of Bytes
MyTurtle.GIF 2019-02-25 00:00:00 6000
Smokey.GIF 2019-05-11 00:00:00 4000
Related
The place where I put this code belongs to an import page.And here there is data in the data I want to import in .txt format, but this data contains the \n character.
if request.method == "POST":
txt_file = request.FILES['file']
if not txt_file .name.endswith('.txt'):
messages.info(request,'This is not a txt file')
data_set = csv_file.read().decode('latin-1')
io_string = io.StringIO(data_set)
next(io_string)
csv_reader = csv.reader(io_string, delimiter='\t',quotechar="|")
for column in csv_reader:
b = Module_Name(
user= request.user,
a = column[1],
b = column[2],
c = column[3],
d = column[4],
e = column[5],
f = column[6],
g = column[7],
h = column[8],
)
b.save()
messages.success(request,"Successfully Imported...")
return redirect("return:return_import")
This can be called the full version of my code. To explain, there is a \n character in the data that comes here as column[1]. This file is a .txt file from another export. And in this export column[1];
This is
a value
and my django localhost new-line character seen in unquoted field - do you need to open the file in universal-newline mode? gives a warning and aborts the import to the system.
the csv reader iterates over rows, not columns. So if you want to append the data from a given column together, you must iterate over all the rows first. For example:
import csv
from io import StringIO
io_string = "this is , r0 c1\r\na value, r1 c2\r\n"
io_string = StringIO(io_string)
rows = csv.reader(io_string)
column_0_data = []
for row in rows:
column_0_data.append(row[0])
print("".join(column_0_data))
the rest of your code looks iffy to me, but that is off topic.
I've written this to do some automated file clean-up on my school folder but the last bit of code at the bottom isn't returning anything when what I want to get is the year that the folder was made.
right now the terminal is giving me this:
Yr 10 Cdate.txt
0
where the last line is blank, but it should be 2020.
code:
import os
from datetime import datetime
folder = r"C:\Users\placeholder\OneDrive\aa.School\Yr 10"
line2 = "Yr 10"
creationtime = os.stat(folder).st_ctime
Ctime = datetime.fromtimestamp(creationtime)
c = line2, " Cdate.txt"
fname = ''.join(c)
d = open(fname, "w")
d.write(str(Ctime))
print(fname)
#checks txt files to see time data
e = open(fname)
print(e.tell())
e.seek(0)
print(e.read(4))
Got two excel files, one with a little ammount of id's and other with tonns of id's + ip address:
How do i compare them line by line, and then print out a concatenation of id(first file) and id+ip(second file) cells that matched with id's?
Where could i go further from here?
import re
router_id = r'[0-9]{5}' # regex for finding 5-num-symboled id like '65432'
ip_multi = r'[0-9]+(?:\.[0-9]+){3}' # for finding ip address
def parsing_func(filename, method):
with open(filename, 'r') as file:
lines = str(file.readlines())
exp_data = re.findall(method, lines)
return exp_data
table_id = set(parsing_func('file1.txt', router_id))
table_addr = set(parsing_func('file2.txt', router_id))
table_mk = set(parsing_func('file2.txt', ip_multi))
content = '\n'.join(table_id)
print(content)
im building a system to sort some .log files in .txt format so that I later can send it to excel. There is 70+ files and in every file im scanning for a keyword, I get 1000+ strings that I want to save in a .txt. I can get every string that I want and see from which .log file each log has ben taken, but now I want to rename the file that the .log came from with a corresponding number 1,2,3,4,5...(one number for every file instead for its file name). code:
import glob
def LogFile(filename, tester):
message = []
data = []
print(filename)
with open(filename) as filesearch: # open search file
filesearch = filesearch.readlines() # read file
i = 1
d = {}
for filename in filename:
if not d.get(filename, False):
d[filename] = i
i += 1
for line in filesearch:
if tester in line: # extract ""
start = '-> '
end = ':\ '
number = line[line.find(start)+3: line.find(end)] #[ord('-> '):ord(' :\ ')]
data.append(number) # store all found wors in array
text = line[line.find(end)+3:]
message.append(text)
with open('Msg.txt', 'a') as handler: # create .txt file
for i in range(len(data)):
handler.write(f"{i}|{data[i]}|{message[i]}")
# open with 'w' to "reset" the file.
with open('Msg.txt', 'w') as file_handler:
pass
# ---------------------------------------------------------------------------------
for filename in glob.glob(r'C:\Users\FKAISER\Desktop\AccessSPA\SPA\*.log'):
LogFile(filename, 'Sending Request: Tester')
I have tried using this function
i = 1
d = {}
for filename in filename:
if not d.get(filename, False):
d[filename] = i
i += 1
but then it looks like this
i want each file to have the same number as in the picture, 1 indicates for all the 26 logs and 2 indicates for the 10 file in that folder... etc
With the following code I'm trying to grab data from a website every 5 mins, timestamp it, calculate its logn return and append all that to a csv file.
Grabbing the data, time stamping it and appending to csv works, but when I try to figure out how to include the log return I'm kind of stuck.
import time
from time import strftime, gmtime
import numpy as np
import urllib2
from urllib2 import urlopen
from math import log
coiAr = []
dateAr = []
logReAr = []
def mcapGrab():
while True:
try:
sourceCode = urllib2.urlopen('http://coinmarketcap.com').read()
mcapUSD = sourceCode.split('<strong>Total Market Cap: <span id="total-marketcap" data-usd="')[1].split('"')[0]
coiAr.append(float(mcapUSD.replace(',','')))
date = strftime('%d %b %Y %H:%M:%S', gmtime())
dateAr.append(date)
# if len(coiAr) > 0:
# indexLog = 1
# logRe = log(coiAr[indexLog]/coiAr[indexLog-1])
# logReAr.append(logRe)
# indexLog += 1
# else:
# logReAr.append(0)
for eachMcap in coiAr:
saveLine = date+','+str(eachMcap)+'\n'
saveFile = open('mcapdata.csv', 'a')
saveFile.write(saveLine)
saveFile.close()
s = 0
print dateAr[s]+','+str(coiAr[s])
time.sleep(300)
s+= 1
except Exception, e:
print 'Failed to grab market cap', str(e)
mcapGrab()
I've commented out the section where I attempt to calc and append log return but doesn't work.
Any help would be appreciated!
Don't use global lists; just write each entry to the file as you find it. Using the csv module would make this all a bit easier still:
import csv
sourceCode = urllib2.urlopen('http://coinmarketcap.com').read()
mcapUSD = sourceCode.split('<strong>Total Market Cap: <span id="total-marketcap" data-usd="')[1].split('"')[0]
mcap = float(mcapUSD.replace(',','')
# read previous value from the CSV first
with open('mcapdata.csv', 'rb') as infh:
last = None
for row in csv.reader(infh):
last = row[1] # second column
# Now calculate the log value based on the last value
logRe = log(mcap/float(last))
# write new values
with open(mcapdata.csv', 'ab') as outfh:
date = strftime('%d %b %Y %H:%M:%S', gmtime())
csv.writer(outfh).writerow([date, mcap, logRe])
This code will read the mcapdata.csv file, picking out just the last value written to it. You could instead also keep all of the rows in memory and just picking out the last entry in a list of lists.