I've got two text files that I want to make a new file from.
One file has data that looks like this: mf-bom.xyrs
J25 5193.94 -3669.45 90.0 1 1 0.0 0.0 DF40C-100DS-0.4V 1 DF40C-100DS-0.4V
J24 4778.15 -3592.17 270.0 1 1 0.0 0.0 DF40C-100DS-0.4V 1 DF40C-100DS-0.4V
TP47 5232.28 -3688.98 0.0 2 1 0.0 0.0 DO NOT POPULATE 1 DNP
TP19 4905.98 -3583.0 0.0 2 1 0.0 0.0 DO NOT POPULATE 1 DNP
TP15 5206.3 -3796.85 0.0 2 1 0.0 0.0 DO NOT POPULATE 1 DNP
and the other has data that looks like this: single-bom.bom
114.119, 114.119, "R12"
114.633, 114.633, "R25"
117.028, 117.028, "C20"
135.495219, 135.495219, "TP34"
137.56, 137.56, "J24"
147.56, 137.56, "J25"
I want to look at the last item in single-bom.bom
and get the value there...In this case, it's R12. Then, I want to look at the first entries in each line of mf-bom.xyrs
and see if there's a match ("R12"). If there is, copy and append that whole line from mf-bom.xyrs
to another file.
I've tried bringing them both into python with a list (eg)
with open('mf-bom.xyrs') as f:
mf_bom = f.read().split("\t")
But this made a long, 1 dimensional list and didn't preserve the lines (newlines, I mean).
Everything else I've tried is too stupid to post here....I feel like I'm banging my head here and missing the point of how easy python can be if you use it right.
This isn't a complete answer, but I wanted to address your attempt to read in the file.
When you do this:
with open('mf-bom.xyrs') as f:
mf_bom = f.read().split("\t")
You're reading in the entire file and then splitting it on tabs...so of course you get one big single list. You want to split each line of the file independently, so something like this:
with open('mf-bom.xyrs') as f:
mf_bom = []
for line in f:
mf_bom.append(line.split('\t'))
After this code, mf_bom
would be a list of lists, one per line, eg:
[
['J25', '5193.94', '-3669.45', '90.0', '1', '1', '0.0', '0.0', 'DF40C-100DS-0.4V', '1', 'DF40C-100DS-0.4V'],
...
]
You can write that more succinctly like this:
with open('mf-bom.xyrs') as f:
mf_bom = [line.split('\t') for line in f]
You could also use the csv
module to read this file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.