You are not logged in.
Pages: 1
i need help on making a script that can change a database txt file that has data like the following
change something like this
data1 data2 data3 data4
--------------------------------------------------------------------------
90 60 03 04
data5 data6 data7 data8
--------------------------------------------------------------------------
05 77 07 08
to look like something like
data1 data2 data3 data4
--------------------------------------------------------------------------
addthis(90) addthis(60) addthis(03) addthis(04)
data5 data6 data7 data8
--------------------------------------------------------------------------
addthis(05) addthis(77) addthis(07) addthis(08)
any hints or know of a script that can do what i need?
Last edited by delacruz (2008-06-12 18:17:07)
Offline
well i did the following
put data in a file called test then
awk '/[0:9]/ {print "addthis(" $1 ")" "" "addthis(" $2 ")" "" "addthis(" $3 ")" "" "addthis(" $4 ")" }' test > test2
the the output (test2) gives
addthis(90)addthis(60)addthis(03)addthis(04)
addthis(05)addthis(77)addthis(07)addthis(08)
this is good enough for me
Last edited by delacruz (2008-06-12 18:22:58)
Offline
#!/usr/bin/env python
# addthis.py - you need python installed (pacman -S python)
# Usage:
# Option 1.
# $ python addthis.py datafile
# Option 2.
# $ chmod +x addthis.py
# $ ./addthis.py datafile
from sys import argv
if argv[0] == "python":
filepath = argv[2]
else:
filepath = argv[1]
inFile = open(filepath,'r')
inText = inFile.readlines()
inFile.close()
outFile = open(filepath+".pydb",'a') # please change this
Nums = ['1','2','3','4','5','6','7','8','9','0']
dont = False
for line in inText:
if line.find("data") > 0 or line.find("-") > 0:
outFile.write(line)
else:
for char in line:
if char in Nums:
if dont == False:
outFile.write("addthis(%s" % char)
dont = True
continue
if dont == True:
outFile.write("%s)" % char)
dont = False
continue
if char not in Nums:
outFile.write(char)
outFile.close()
This is pretty ugly python, about as bad as it gets... But it get's the job done perfectly .
Last edited by vsk (2008-06-13 00:36:13)
Offline
vsk thank you, it works :-)
EDIT:
vsk i have been trying to do this on my own but i cant get it work
this is part of my data
The following COMMON items have been defined. They occur on all records.
--------------------------------------------------------------------------------
Item (occurs) Data Item
Subitem (occurs) Type Position Len. Value Name Values
--------------------------------------------------------------------------------
(record type) A 1-2 2
LOTE N 3-5 3
TIPO-DE-HOGAR N 6 1 0:4
FOLIO N 7-11 5 00000:99999
REGION N 12 1 OCCIDENTAL 1
CENTRAL I 2
CENTRAL II 3
ORIENTAL 4
AMSS 5
AREA N 13 1 URBANO 1
RURAL 2
i need my data to look like the following
dictionary {
_column(3) long lote %3f
_column(6) long tipo-de-hogar %1f
_column(7) long folio %5f
_column(12) long region %1f
_column(13) long area %1f
}
i dont understand what your script is doing so im not able to mod it myself
Last edited by delacruz (2008-06-13 02:45:57)
Offline
Right, I'm sorry, but what to 'leave out' of the charecter by charecter line processing is hardcoded in -
try changing
if line.find("data") > 0 or line.find("-") > 0:
# to
items = ["data","-"] # add on to the blacklist
for item in items:
if line.find(item) > 0:
outFile.write(line)
break
to exclude more lines that should not change.
Detection for the _columns is done by the for loop and the Nums array, so you'd need to play around with that.
I'd help you with that, but I'm leaving to go to NY in 30 mins, so sorry!
Offline
Ok, I'm back. So let me explain my crappy script. This chunk is what the whole thing revolves on, so let me explain it;
for line in inText: # inText is an array of strings. Every item in this array is exactly ONE line of text from the input file.
# now, this first for loop takes one item at a time in the inText array and assigns it to the variable "line". Every repetition moves on to
# the next item in the array, until there are no more items (i.e until your entire file has been processed.)
if line.find("data") > 0 or line.find("-") > 0: # this is the blacklisting I was talking about earlier. if ANY string item in the inText array
# contains the string "data" or the string "-" in it, this will by default ('without asking any questions') write that line to the new file.
# after doing so, the loop will move on to the next item in the inText array
outFile.write(line)
else: # if it does not have those strings, then it will probe the string for an integer
for char in line: # for every character in the string line
if char in Nums: # if that charecter is an integer
if dont == False: # check if it is the first in the set of 2 integers. if it is, then do this:
outFile.write("addthis(%s" % char)
dont = True # then, alert the script that in the next iteration of the loop, we will have to deal with the SECOND integer in the set
# of two integers
continue
if dont == True: # check if this is the second in the set of two integers, if so, do this:
outFile.write("%s)" % char)
dont = False # reset the script so that it doesn't perpetually append char) to every integer
continue
if char not in Nums: # however, if the charecter in the line is not a number, go ahead and write it to the file
outFile.write(char)
and thats it.
Unfortunately, I have commited almost every Pythonic sin;
$ python -c "import this"
and you'll know what I mean.
I have to admit that your last post absolutely baffles me - I don't know what to do with that... Sorry.
Offline
thanks vsk i will look at your post and study it and hopefully i can make it do what i need it to do.
Offline
Pages: 1