How to get data from txt file in python log analysis? - python

I am beginner to python, I am trying to do log analysis, but I do not know how to get the txt file.
This is the code for outputting date, but these dates must be taken from the txt file :
import sys
import re
file = open ('desktop/trail.txt')
for line_string in iter(sys.stdin.readline,''):
line = line_string.rstrip()
date = re.search(r'date=[0-9]+\-[0-9]+\-[0-9]+', line)
date = date.group()
print date

You can use with statement to open a file safely and read each line with a readlines method. readlines returns a list of string.
Below code should work in your case:
import sys
import re
with open('desktop/trail.txt', 'r') as f:
for line in f.readlines():
line = line_string.rstrip()
date = re.search(r'date=[0-9]+\-[0-9]+\-[0-9]+', line)
date = date.group()
print date

you can do something like
for line in file.readlines():
don't forget about file closing! You can do it with file.close()

Related

Reading Specific String from a line in a file

I have file looking like this:
face LODRxERROR
{
source R/com/int/LRxAMEexception.csv
contains R/saqf/LAWODRxERROR.ddf
contains R/bld/LAWODRxERRORtyp.h
contains R/bld/LAWODRxERRORtyp.hpp
requires LAWODRxERR
}
At the moment I'm able to read a specific line and store it. But I need to be more specific. Instead of reading the whole line. I would like to read only the file name no the directory. So, instead of reading R/bld/LAWODRxERRORtyp.hpp I would like to read only LAWODRxERRORtyp.hpp
Here is my python code so far:
with open(file) as scope:
for line in scope:
line = line.strip()
if line.startswith('contains') and line.endswith('.h') or line.endswith('.hpp'):
scopeFileList.append(line.split()[-1])
Thanks in advance
You can use the built-in function os.path.basename() to get only the file-name from a path:
from os.path import basename
with open(file) as scope:
for line in scope:
line = line.strip()
if line.startswith('contains') and line.endswith('.h') or line.endswith('.hpp'):
path = line.split()[-1]
scopeFileList.append(basename(path))
Try this,
with open("file1.txt", "r") as f:
data = [line.replace("\n","").split('/')[-1] for line in f.readlines() if '.' in line]
Output:
print(data)
['LRxAMEexception.csv',
'LAWODRxERROR.ddf',
'LAWODRxERRORtyp.h',
'LAWODRxERRORtyp.hpp']
Try this: You can use the re.search to find the file names from a path
with open('new_file.txt') as file:
for line in file:
line = line.strip()
if line.startswith('source') or line.startswith('contains'):
file_names = re.search('/(.+?)\/((\w+)\.\w+$\Z)', line).group(2)
print(file_names)
O/P:
'LRxAMEexception.csv'
'LAWODRxERROR.ddf'
'LAWODRxERRORtyp.h'
'LAWODRxERRORtyp.hpp'

Python - Add line to top of file being written

I am scraping info to a text file and am trying to write the date at the top. I have the method to grab the date but have no clue how I can use the write function to place at top. Below is a stripped down version of what I am working on.
import re
import urllib2
import json
from datetime import datetime
import time
now = datetime.now()
InputDate = now.strftime("%Y-%m-%d")
Today = now.strftime("%B %d")
header = ("Today").split()
newfile = open("File.txt", "w")
### Irrelevant Info Here ###
string = title"\n"+info+"\n"
#newfile.write(header)
newfile.write(string)
print title+" written to file"
newfile.close()
You can't insert something at the beginning of a file. You need to write a new file, starting with the line you want to insert, then finish with the contents of the old file. Unlike appending to the end, writing to the start of the file is really, really inefficient
The key to this problem is to use a NamedTemporaryFile. After you finish constructing it, you then rename it on top of the old file.
Code:
def insert_timestamp_in_file(filename):
with open(filename) as src, tempfile.NamedTemporaryFile(
'w', dir=os.path.dirname(filename), delete=False) as dst:
# Save the new first line
dst.write(dt.datetime.now().strftime("%Y-%m-%d\n"))
# Copy the rest of the file
shutil.copyfileobj(src, dst)
# remove old version
os.unlink(filename)
# rename new version
os.rename(dst.name, filename)
Test Code:
import datetime as dt
import tempfile
import shutil
insert_timestamp_in_file("file1")
file1
I am scraping info to a text file and am trying to write the date at
the top. I have the method to grab the date but have no clue how I can
use the write function to place at top. Been trying for 2 days and all.
Results:
2018-02-15
I am scraping info to a text file and am trying to write the date at
the top. I have the method to grab the date but have no clue how I can
use the write function to place at top. Been trying for 2 days and all.
To write the date to the 'top' of the file you would want to put:
newfile.write(InputDate)
newfile.write(Today)
after where you open the file and before anything else.
Just to give you idea
Try this:-
import re
import urllib2
import json
from datetime import datetime
import time
now = datetime.now()
InputDate = now.strftime("%Y-%m-%d")
Today = now.strftime("%B %d")
#start writing from here
newfile = open("File.txt", "a")
newfile.write(InputDate+"\n")
newfile.write("hello Buddy")
newfile.close()
Simple One will be, if you will not call it as a str then it will throw an error TypeError: write() argument must be str, not list
I have rfreshed teh code to be more precise and effective use..
import re
from datetime import datetime
import time
now = datetime.now()
InputDate = now.strftime("%B"+" "+"%Y-%m-%d")
newfile = open("File.txt", "a")
string = "Hi trying to add a datetime at the top of the file"+"\n"
newfile.write(str(InputDate+"\n"))
newfile.write(string)
newfile.close()
Result will be:
February 152018-02-15
Hi trying to add a datetime at the top of the file

Import file with string python for certain condition

I am trying to import a txt file to a list in python.
What am I doing right now
with open('my_connection_page.txt', 'r') as f:
url = f.readlines()
It just put everything into the url[0].
This is the Text file
[u'/scheck/', u'/amanda/', u'/in/amanda/', u'/462aa6aa/', u'/462aa6aa/', u'/895161106/', u'/895161106/', u'/anshabenhudson/']
What should I do?
Use url = f.read().split() instead. You can use delimiter in split().

Parsing a line from an ASCII HDR file python

I am having difficulty parsing a line from an hdr file I have. When I print read (data) like in the code below the command window outputs the contents of the hdr file. However, when I try to parse out a line or a column , like the script below, it outputs nothing in the command window.
import numpy as np
import matplotlib.pyplot as plt
f = open('zz_ssmv11034tS__T0001TTNATS2012021505HP001.Hdr', 'r')
data = f.read()
print (data)
for line in f:
columns = line.split()
time = float(columns[2])
print (time)
f.close()
Remove this two lines and execute your code again:
data = f.read()
print (data)
Then change your loop:
for line in f.readlines():
columns = line.split()
time = float(columns[2])
print (time)
Calling read() reads through the entire file and leaves the read cursor at the end of the file (with nothing more to read). If you are looking to read a certain number of lines at a time you could use readline(), readlines()
Read the post Why can't I call read() twice on an open file?

how to choose and upload a file in python

I am writing a program where it asks you what text file the user wants to read then it begins to read whatever file name the user inputs. Here is what I have so far:
import sys
import os
import re
#CHOOSE FILE
print "Welcome to the Parsing Database"
raw_input=raw_input("enter file name to parse: ")
#ASSIGN HEADERS AND SEQUENCES
f=open("raw_input", "r")
header=[]
sequence=[]
string=""
for line in f:
if ">" in line and string=="":
header.append(line[:-2])
elif ">" in line and string!="":
sequence.append(string)
header.append(line[:-2])
string=""
else:
string=string+line[:-2]
sequence.append(string)
The first two lines work but then it says it cannot find the file that I inputted to read. Please help! Thanks.
Off the top of my head, I think that f = open("raw_input", "r") needs to be f=open(raw_input, "r"), because you are trying to reference the string contained in the variable raw_input, as opposed to trying to open a file named raw_input. Also you should probably change the name of the variable to something more readable, because raw_input() is a function used in your code as well as a variable, which makes it hard to read. Are there any other specific problems you are having with your code?
f=open("raw_input", "r")
"raw_input" is a plain string. You have to referente to it as raw_input.
Also, there's no lines if you don't use .read() with open() method so you can't parse them. Read lines from a file given from raw_input can be done doing that:
import sys
import os
import re
#CHOOSE FILE
print "Welcome to the Parsing Database"
raw_input_file=raw_input("enter file name to parse: ")
#ASSIGN HEADERS AND SEQUENCES
testfile = open(raw_input_file, "r")
secuence = []
for line in testfile.read().splitlines():
secuence.append(line)
for i in secuence:
print i
testfile.close()

Categories

Resources