asked on
ASKER
ASKER
File "<ipython-input-9-9b299f36369a>", line 4
print 'process_file( "%s" )' % file_name
^
SyntaxError: invalid syntax 4 def parse_line(line):
5 items = {'Staff No', 'Name', 'Department', 'Join Date', 'Relieve Date'}
----> 6 for item in items.keys() :
7 pos = line.find( item )
8 if not items[ item ] and pos > -1 :
--------------------------import glob
import pyodbc
def parse_line( line ):
#---------------------------------------------------------------------------
# items is a dictionary, each value of which is indexed by a key
# The items dictionary is initialized so that each value is None.
#---------------------------------------------------------------------------
items = { 'Staff No' : None
, 'Name' : None
, 'Department' : None
, 'Join Date' : None
, 'Relieve Date' : None
}
#---------------------------------------------------------------------------
# For each dictionary key, see if it exists in the current line
#---------------------------------------------------------------------------
for item in items.keys() :
pos = line.find( item )
#-----------------------------------------------------------------------
# "not items[ item ]" is True if the specified dictionary value has not
# yet been located and extracted from the line.
#-----------------------------------------------------------------------
if not items[ item ] and pos > -1 :
#-------------------------------------------------------------------
# if pos > -1, then the dictionary key (in the variable "item") was
# found in the current line. For example:
#-------------------------------------------------------------------
# line = "Emp No: 101 Name: RASUL K SAL: 30000"
# -----+----1----+--^-2-- ^ ^
# | |
# The value we want is from here: +------------+ to here
# item = "Name"
# pos = 18
# len( item ) = 4
# pos + len( item ) = 22 << This really should be 24!
# pos + len( item ) + 15 << This really should be 37!
#-------------------------------------------------------------------
# val = line[ pos + len( item ) + 2 : pos + len( item ) + 15 ].strip()
val = line[ pos + len( item ) + 4 : pos + len( item ) + 13 ].strip()
#-------------------------------------------------------------------
# This expects the located value to end just before the tab char.
#-------------------------------------------------------------------
tab = val.find( '\t' )
if tab > -1 :
val = val[ : tab ].strip()
items[ item ] = val
return items
def process_file( file_name ):
#---------------------------------------------
# The default open type is "read", so there is
# no need to specify 'r' in the open call.
#---------------------------------------------
with open( file_name ) as f:
data = f.readlines()
#---------------------------------------------
# If you really don't want to display the file
# contents, comment out the whole loop.
#---------------------------------------------
# linenum = 0
# for line in data :
# linenum += 1
# print '%2d: "%s"' % ( linenum, line.strip() )
return parse_line(line)
def write_db(data, server, database, username, password):
connection_string = ( 'DRIVER={SQL Server Native Client 11.0};SERVER='
+ server
+ ';DATABASE='
+ database
+ ';UID='
+ username
+ ';PWD='
+ password
)
with pyodbc.connect( connection_string, autocommit=False ) as cnxn:
cur = cnxn.cursor()
print( data )
sql = "INSERT INTO Emp (Staffno, EmpName, Department) VALUES (?, ?, ?)"
cur.executemany( sql, data )
# ==> TEST FILE Link (https://1drv.ms/f/s!AiSRcgO5FUmNdbySD6gz4PwTbzk)
pattern = 'D:/Test/Python/Proj/*.txt'
files = glob.glob( pattern )
all_data = [ process_file( file_name ) for file_name in files ]
server = 'SERVERXYZ'
database = 'TEST'
username = 'TEST123'
password = 'TEST123'
#----------------------------------------
# write_db( data=all_data
# , server=server
# , database=database
# , username=username
# , password=password
# ) # replace srever, database, password with actual values
#----------------------------------------
ASKER
ASKER
import glob
import pyodbc
def parse_line( line ):
#---------------------------------------------------------------------------
# items is a dictionary, each value of which is indexed by a key
# The items dictionary is initialized so that each value is None.
#---------------------------------------------------------------------------
items = { 'Staff No' : None
, 'Name' : None
, 'SAL' : None
, 'Join Date' : None
, 'Relieve Date' : None
}
#---------------------------------------------------------------------------
# For each dictionary key, see if it exists in the current line
#---------------------------------------------------------------------------
for item in items.keys() :
pos = line.find( item )
#-----------------------------------------------------------------------
# "not items[ item ]" is True if the specified dictionary value has not
# yet been located and extracted from the line.
#-----------------------------------------------------------------------
if not items[ item ] and pos > -1 :
#-------------------------------------------------------------------
# if pos > -1, then the dictionary key (in the variable "item") was
# found in the current line. For example:
#-------------------------------------------------------------------
# line = "Emp No: 101 Name: RASUL K SAL: 30000"
# -----+----1----+--^-2-- ^ ^
# | |
# The value we want is from here: +------------+ to here
# item = "Name"
# pos = 18
# len( item ) = 4
# pos + len( item ) = 22 << This really should be 24!
# pos + len( item ) + 15 << This really should be 37!
#-------------------------------------------------------------------
#val = line[ pos + len( item ) + 2 : pos + len( item ) + 15 ].strip()
val = line[ pos + len( item ) + 4 : pos + len( item ) + 13 ].strip()
#-------------------------------------------------------------------
# This expects the located value to end just before the tab char.
#-------------------------------------------------------------------
tab = val.find( '\t' )
if tab > -1 :
val = val[ : tab ].strip()
items[ item ] = val
return items
def process_file( file_name ):
#---------------------------------------------
# The default open type is "read", so there is
# no need to specify 'r' in the open call.
#---------------------------------------------
with open( file_name ) as f:
data = f.readlines()
#---------------------------------------------
# If you really don't want to display the file
# contents, comment out the whole loop.
#---------------------------------------------
linenum = 0
for line in data :
linenum += 1
print( linenum, line.strip() )
return parse_line(line)
def write_db(data, server, database, username, password):
connection_string = ( 'DRIVER={SQL Server Native Client 11.0};SERVER='
+ server
+ ';DATABASE='
+ database
+ ';UID='
+ username
+ ';PWD='
+ password
)
with pyodbc.connect( connection_string, autocommit=False ) as cnxn:
cur = cnxn.cursor()
print( data )
sql = "INSERT INTO test1 (Staffno, EmpName, SAL, JoinDate, RelieveDate) VALUES (?, ?, ?, ?, ?)"
cur.executemany( sql, data )
pattern = 'C:/Test/Python/Proj/*.txt'
files = glob.glob( pattern )
all_data = [ process_file( file_name ) for file_name in files ]
server = 'SERVERXYZ'
database = 'TEST'
username = 'TEST123'
password = 'TEST123'
write_db(data=all_data, server=server, database=database, username=username, password=password) # replace srever, database, password with actual values
# line = "Emp No: 101 Name: RASUL K SAL: 30000"
# -----+----1----+--^-2-- ^ ^
# | |
# The value we want is from here: +------------+ to here
# item = "Name"
# pos = 18
# len( item ) = 4
# pos + len( item ) = 22 << This really should be 24!
# pos + len( item ) + 15 << This really should be 37
ASKER
ASKER
import glob
# import pyodbc
def parse_line( line ):
#---------------------------------------------------------------------------
# items is a dictionary, each value of which is indexed by a key
# The items dictionary is initialized so that each value is None.
#---------------------------------------------------------------------------
items = { 'Staff No' : None
, 'Name' : None
, 'Department' : None
, 'Join Date' : None
, 'Relieve Date' : None
}
#---------------------------------------------------------------------------
# For each dictionary key, see if it exists in the current line
#---------------------------------------------------------------------------
for item in items.keys() :
pos, L = line.find( item ), len( item )
#-----------------------------------------------------------------------
# Does line contain the current item?
#-----------------------------------------------------------------------
if not items[ item ] and pos > -1 :
start, finish = pos + L + 1, pos + L + 13
print( 'item = "%s" pos = %d line[ %d:%d ] = "%s"' % (
item
, pos
, start
, finish
, line[ start : finish ]
)
)
val = line[ start : finish ].strip()
#-------------------------------------------------------------------
# This expects the located value to end just before the tab char.
#-------------------------------------------------------------------
tab = val.find( '\t' )
if tab > -1 :
val = val[ : tab ].strip()
items[ item ] = val
return items
def process_file( file_name ):
print( 'Processing:', file_name )
#---------------------------------------------
# The default open type is "read", so there is
# no need to specify 'r' in the open call.
#---------------------------------------------
with open( file_name ) as f:
data = f.readlines()
#---------------------------------------------
# If you really don't want to display the file
# contents, comment out the whole loop.
#---------------------------------------------
linenum = 0
results = {}
for line in data :
linenum += 1
print( '%2d: "%s"' % ( linenum, line.strip() ) )
info = parse_line( line )
for item in info.keys() :
if info[ item ] :
results[ item ] = info[ item ]
print( 'results:', results )
return results
# def write_db(data, server, database, username, password):
# connection_string = ( 'DRIVER={SQL Server Native Client 11.0};SERVER='
# + server
# + ';DATABASE='
# + database
# + ';UID='
# + username
# + ';PWD='
# + password
# )
# with pyodbc.connect( connection_string, autocommit=False ) as cnxn:
# cur = cnxn.cursor()
# print( data )
# sql = "INSERT INTO Emp (Staffno, EmpName, Department) VALUES (?, ?, ?)"
# cur.executemany( sql, data )
# ==> TEST FILE Link (https://1drv.ms/f/s!AiSRcgO5FUmNdbySD6gz4PwTbzk)
pattern = '*.txt'
files = glob.glob( pattern )
all_data = [ process_file( file_name ) for file_name in files ]
print( "all_data:", all_data )
# server = 'SERVERXYZ'
# database = 'TEST'
# username = 'TEST123'
# password = 'TEST123'
#----------------------------------------
# write_db( data=all_data
# , server=server
# , database=database
# , username=username
# , password=password
# ) # replace srever, database, password with actual values
#----------------------------------------
ASKER
print( 'type( data ):', type( data ) )
print( data )
def write_db(data, server, database, username, password):
print( 'type( data ():', type( data ) )
print( data )
connection_string = ( 'DRIVER={SQL Server Native Client 11.0};SERVER='
+ server
+ ';DATABASE='
+ database
+ ';UID='
+ username
+ ';PWD='
+ password
)
for entry in data :
print( 'type( entry ):', type( entry ) )
print( entry )
with pyodbc.connect( connection_string, autocommit=False ) as cnxn:
cur = cnxn.cursor()
print( data )
sql = "INSERT INTO Emp (Staffno, EmpName, Department) VALUES (?, ?, ?)"
cur.executemany( sql, entry )
ASKER
ASKER
ASKER
A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. Thousands of different programming languages have been created, mainly in the computer field, and many more still are being created every year. The description of a programming language is usually split into the two components of syntax (form) and semantics (meaning). Some languages are defined by a specification document (for example, the C programming language is specified by an ISO Standard), while other languages (such as Perl) have a dominant implementation that is treated as a reference. Some languages have both, with the basic language defined by a standard and extensions taken from the dominant implementation being common.
TRUSTED BY
Open in new window
Sample output:
--------------------------
files: ['sample.txt']
process_file( "sample.txt" )
1: "Please find below emp details,"
2: ""
3: "EMP DETAILS"
4: "_________________________
5: ""
6: "Emp No: 101 Name: RASUL K SAL: 30000"
7: "_________________________
8: ""
9: "Join Date: 10 JAN 2010 From Native: First"
10: "Relieve Date: 25 APR 2018 To Native: Second"
11: "EmpRole: Manager"
12: "No.ofProjects handled: 10"
{'Department': None, 'Staff No': None, 'Relieve Date': '25 APR 2018', 'Name': 'RASUL K', 'Join Date': '10 JAN 2010'}