python - How to .replace from a line to another in a .txt, if the line to be replaced is above the line where i'll get the value for replacement? -


first of all, sorry long title.. couldn't explain in simple way.. feel free edit it!

i need logic solve problem...

i have .txt file following characteristics:

  1. each line corresponds specific operation...
  2. each field of line delimited "|" separator
  3. my .txt have 120k+ lines, 60k~ invoices
  4. i have copy values lines starts c195, , replace them in specific field of c100 line immediatly above c195 line.

example:

the invoice's part of .txt like:

  • |c100|1|1238761|128,82|1002,21|0,00|0,00|0,00|0,00|
  • |c170|1|414859|mini leitoa|kg|21,80|kg|
  • |c190|363,53|0,00|0,00|0,00|0,00|0,00|0,00|
  • |c195|c195|1|base de cálculo st: 193,56 - valor da st: 10,10|
  • |c195|c195|2|valor ipi: 7,10|

what supposed be:

  • |c100|1|1238761|128,82|1002,21|0,00|193,56|10,10|7,10|
  • |c170|1|414859|mini leitoa|kg|21,80|kg|
  • |c190|363,53|0,00|0,00|0,00|0,00|0,00|0,00|
  • |c195|1|base de cálculo st: 193,56 - valor da st: 10,10|
  • |c195|2|valor ipi: 7,10|

what did until now:

  1. created program read lines of txt , store them in input_lines = []
  2. get index() of lines in input_lines starts c100, , store them in pos_c100 = []
  3. since c195 field can have either values of tax1 (icms) or tax2 (ipi), used re.search(param,string) find if line contains either "icms" or "ipi".
  4. if line contains "icms", contains 2 values: first 1 icms_basis , other icms_value
    • |c195|1|base de cálculo st: 193,56 - valor da st: 10,10|
  5. if line contains "ipi", contains 1 value: ipi_value
    • |c195|1|valor ipi: 10,10|
  6. i've extracted values out of string using re.findall() , stored them in "tax specific dictionary" line position

since each value have specific position replaced, , know these positions, created 2 dictionaries hold index of c195 line , values, 1 dic ipi , icms.

my data layout:

pos_c100 = [line_c100] dic_icms = {line_c195 : [icms_basis, icms_value]} dic_ipi = {line_c195 : ipi_value} 

what got after running script now, example:

input_lines = ["|c100|1..", "|c170|1..", "|c195|1|ipi..", "c195|1|icms.."] #the output of `file.readlines()` pos_c100 = [2, 4, 8, 10] #the positions of lines start c100 dic_icms = {6 : ["200,15", "15,80"]} #{key, [icms_basis, icms_value]} dic_ipi = {7 : "7,15"} # {key, icms_} #key position of lines startswith c195 in input_lines  

using above dic_icms example:

how can "200,15" , "15,80" dic_icms, located @ line in 6th position of lines_input, , replace in specific position of line in 4th position of lines_input using loop in dictionaries?

i need way check if line closest above , if so, replace value referring dict values...

maybe a

for key in dic_ipi:     item in pos_c100:         dists = []         dist = key - item         dists.append(dist) 

and

linha = (linha[0:posinicialbcicms] + linha[posinicialbcicms:posfinalbcicms].replace("0,00", icms_basis) + linha[posinicialvlricms:posfinalvlricms].replace("0,00", icms_value) + linha[posinicialvlripi:posfinalvlripi].replace("0,00", ipi_value) + linha[posfinalvlripi + 1 : len(linha)]) 

let's start reading file:

file = open("blabla.txt", "r"). data = file.read() file.close() 

out data contains whole text. using split:

data = data.split("\n") #splitting \n 

you big array looking this: ['line1', 'line2', 'line3'...]. need create new table:

new_table = [] 

and it's time heavy operations. since line looks |c170|1|414859|mini leitoa|kg|21,80|kg| can use split() once more:

for line in data:     new_table.append(line.split("|") # split returns new table splitted "|". append adds table new_table in last position. 

new_table looks this: [[line1_element1, line1_element2, line1_element2], [line2_element1, line2_element2, line2_element3],...]. should able use len() function length of specific table. example:

for in xrange(0, len(new_table)): # iterating through lines, 'i' integer type 0 len(new_table)     j in xrange(len(new_table[i])): # iterating through each element in 'i' line (i index of new_table)         print new_table[i][j] # print j element of line in new_table 

if can acces every element in every line it's index, can easli compare using if statement. example:

if new_data[i][j] == "c165":     do_something() 

hope helps. --edit-- asking closest line. i or j element +- 1.


Comments