Python: Copying lines that meet requirements -
so, basically, need program opens .dat file, checks each line see if meets prerequisites, , if do, copy them new csv file.
the prerequisites must 1) contain "$w" or "$s" , 2) have last value @ end of line of dat 1 of long list of acceptable terms. (i can make-up list of terms , hardcode them list)
for example, if csv list of purchase information , last item purchased, want include fruit. in case, last item id tag, , want accept handful of id tags, there list of 5 acceptable tags. tags have veriable length, however, last item in list (and 4th item on list)
let me give better example, again fruit.
my original .dat might be:
dgh$g$h $2.53 london_port gyro dgh.$wfft$q5632 $33.54 55n39 barkdust uykj$s.52ue $23.57 22#3 apple wsiajsm_33$4.fj4 $223.4 ha25%ek banana
only line: "uykj$s $23.57 22#3 apple" copied because has both 1) $w or $s (in case $s) , 2) last item fruit. once .csv file made, going need go through , replace spaces commas, that's not problematic me figuring out how scan each line requirements , copy ones wanted.
i making few programs similar one, open .dat files, check each line see if meet requirements, , decides copy them new file or not. sadly, have no idea doing. similar enough once figure out how make one, rest easy, though.
edit: .dat files few thousand lines long, if matters @ all.
edit2: of current code snippets
right now, current version this:
def main(): #newfile_loc = c:\users\j18509\documents oldfile_loc=raw_input("input file mclg:") oldfile = open(oldfile_loc,"r") oldtext = oldfile.read() # in range(0, len(oldtext)): # if (oldtext[i] != " "): # print oldtext[i] = split_line(oldtext) if u'$s' in i: # $s in line print main()
but it's choppy still. i'm learning python.
brief update: server working on down, , might next few hours, have new code, has syntax errors in it, here anyways. i'll update again once working. bunch everyone!
import os newfilepath = "a:\test.txt" acceptable_values = ('apple','banana') #main def main(): if os.path.isfile(newfilepath): os.remove(newfilepath) newfile = open (newfilepath, 'w') newfile.write('header 1,','name header,','header 3,','header 4) oldfile_loc=raw_input("input file program:") oldfile = open(oldfile_loc,"r") line in oldfile: lineparts = line.split() if (lineparts[0].find($w)) or (lineparts[0].find($s)): if lineparts[3] in acceptable_values: print(lineparts[1], ' accepted') #this line acceptable! newfile.write(lineparts[1],',',lineparts[0],',',lineparts[2],',',lineparts[3]) oldfile.close() newfile.close() main()
there 2 parts need implement: first, read file line line , write lines meeting specific criteria. done by
with open('file.dat') f: line in f: stripped = line.strip() # remove '\n' end of line if test_line(stripped): print stripped # write stdout
the criteria want check implemented in function test_line
. check occurrence of "$w" or "$s", can use in
-operator like
if not '$w' in line , not '$s' in line: return false else: return true
to check, if last item in line contained in fixed list, first split line using split()
, take last item using index notation [-1]
(negative indices count end of sequence) , use in
operator again against fixed list. looks like
items = line.split() # items array of strings last_item = items[-1] # take last element of array if last_item in ['apple', 'banana']: return true else: return false
now, combine these 2 parts test_line
function like
def test_line(line): if not '$w' in line , not '$s' in line: return false items = line.split() # items array of strings last_item = items[-1] # take last element of array if last_item in ['apple', 'banana']: return true else: return false
note program writes result stdout, can redirect. if want write output file, have @ correct way write line file in python
Comments
Post a Comment