Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: Copying lines that meet requirements
    primarykey
    data
    text
    <p>So, basically, I need a program that opens a .dat file, checks each line to see if it meets certain prerequisites, and if they do, copy them into a new csv file.</p> <p>The prerequisites are that it must 1) contain "$W" or "$S" and 2) have the last value at the end of the line of the DAT say one of a long list of acceptable terms. (I can simply make-up a list of terms and hardcode them into a list)</p> <p>For example, if the CSV was a list of purchase information and the last item was what was purchased, I only want to include fruit. In this case, the last item is an ID Tag, and I only want to accept a handful of ID Tags, but there is a list of about 5 acceptable tags. The Tags have very veriable length, however, but they are always the last item in the list (and always the 4th item on the list)</p> <p>Let me give a better example, again with the fruit.</p> <p>My original .DAT might be:</p> <pre><code>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 </code></pre> <p>Only the line: "UYKJ$S $23.57 22#3 Apple" would be copied because only it has both 1) $W or $S (in this case a $S) and 2) The last item is a fruit. Once the .csv file is made, I am going to need to go back through it and replace all the spaces with commas, but that's not nearly as problematic for me as figuring out how to scan each line for requirements and only copy the ones that are wanted.</p> <p>I am making a few programs all very similar to this one, that open .dat files, check each line to see if they meet requirements, and then decides to copy them to the new file or not. But sadly, I have no idea what I am doing. They are all similar enough that once I figure out how to make one, the rest will be easy, though.</p> <p>EDIT: The .DAT files are a few thousand lines long, if that matters at all.</p> <p>EDIT2: The some of my current code snippets</p> <p>Right now, my current version is this:</p> <pre><code>def main(): #NewFile_Loc = C:\Users\J18509\Documents OldFile_Loc=raw_input("Input File for MCLG:") OldFile = open(OldFile_Loc,"r") OldText = OldFile.read() # for i in range(0, len(OldText)): # if (OldText[i] != " "): # print OldText[i] i = split_line(OldText) if u'$S' in i: # $S is in the line print i main() </code></pre> <p>But it's very choppy still. I'm just learning python.</p> <p>Brief update: the server I am working on is down, and might be for the next few hours, but I have my new code, which has syntax errors in it, but here it is anyways. I'll update again once I get it working. Thanks a bunch everyone!</p> <pre><code>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 for Program:") OldFile = open(OldFile_Loc,"r") for 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], ' is accepted') #This Line is acceptable! NewFile.write(LineParts[1],',',LineParts[0],',',LineParts[2],',',LineParts[3]) OldFile.close() NewFile.close() main() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload