'Python 3.8.10 Reading file ignores changes
Hi and thank you in advance! I'm pulling my hair out with a weird issue I have concerning editing a txt file in python. I'm opening reading and edit a file and at some point when I try to write to it again the file is as it was before the edits! I'm no programmer just a scripting guy with lots to learn. Please have you any idea what is going on? The ultimate goal is to detect a line and duplicate it... in this example line 18 of the text file must be duplicated before anything else..
output = 'file1.txt'
with open(output, 'r') as fr:
for line in lines:
if ptr == 9:
heats.append(line[11:19].strip())
if ptr == 10:
heats.append(line[11:19].strip())
if ptr == 11:
heats.append(line[11:19].strip())
if ptr == 12:
heats.append(line[11:19].strip())
if ptr == 13:
heats.append(line[11:19].strip())
if ptr == 16:
chem_heats.append(line)
if ptr == 17:
chem_heats.append(line)
if ptr == 18:
chem_heats.append(line)
if ptr == 19:
chem_heats.append(line)
if ptr == 20:
chem_heats.append(line)
ptr += 1
#find duplicate heats--->
# print(heats)
# print(chem_heats)
seen = set()
dupes = []
ptr = 1
for x in heats:
if x in seen:
dupes.append(x)
else:
seen.add(x)
duplicate=str(dupes)[2:7]
duplicate_instances = len(dupes)
for chem_line in chem_heats:
if chem_line[:5] == duplicate:
os.system("sed -i '/^"+duplicate+".*/a "+chem_line+"' " + output)
ptr = 1
#find duplicate heats<---
openFile = open(output, "r")
readFile = openFile.read()
print (readFile )
with open(output, 'w') as fw:
for line in lines:
if ptr == 7:
newldata = line[0:35]+"\n"
lines[(ptr-1)] = newldata
if ptr == 9:
newldata = line[20:48]+"\n"
heat_1 = line[11:19].strip()
weight_1 = line[52:60].rstrip()
lines[(ptr-1)] = newldata
if ptr == 10:
newldata = line[20:48]+"\n"
heat_2 = line[11:19].strip()
weight_2 = line[52:60].rstrip()
lines[(ptr-1)] = newldata
if ptr == 11:
newldata = line[20:48]+"\n"
heat_3 = line[11:19].strip()
weight_3 = line[52:60].rstrip()
lines[(ptr-1)] = newldata
if ptr == 12:
newldata = line[20:48]+"\n"
heat_4 = line[11:19].strip()
weight_4 = line[52:60].rstrip()
lines[(ptr-1)] = newldata
if ptr == 13:
newldata = line[20:48]+"\n"
heat_5 = line[11:19].strip()
weight_5 = line[52:60].rstrip()
lines[(ptr-1)] = newldata
if ptr == 14:
newldata = " ".join(line[47:].split())+"\n"
lines[(ptr-1)] = newldata
if ptr == 15:
newldata = " ".join(line.split())+"\n"
lines[(ptr-1)] = newldata
if ptr == 16:
newldata = " ".join(line.split())+"\n"
lines[(ptr-1)] = newldata
if ptr == 17:
newldata = " ".join(line.split())+"\n"
lines[(ptr-1)] = newldata
if ptr == 18:
newldata = " ".join(line.split())+"\n"
lines[(ptr-1)] = newldata
if ptr == 19:
newldata = " ".join(line.split())+"\n"
lines[(ptr-1)] = newldata
if ptr == 20:
newldata = " ".join(line.split())+"\n"
lines[(ptr-1)] = newldata
if ptr == 23:
newldata = " ".join(line.split())+"\n"
line_hardness_hdr = newldata.rstrip()
lines[(ptr-1)] = newldata
if ptr in range(25,35): #mechanical properties
newldata = line[4:45]+"\n"
lines[(ptr-1)] = newldata
ptr += 1
fw.writelines(lines)
os.system("sed -i '35,$d' " + output)
os.system("sed -i '/^[[:space:]]*$/d' " + output)
os.system("sed -i '5,6d' " + output)
os.system("sed -i '6d' " + output)
os.system("sed -i '12d' " + output)
os.system("sed -i '17,20d' " + output)
#<-- Clean up ends
#--> Generate variables
with open(output, 'r') as fr:
# reading line by line
lines = fr.readlines()
line_flds = 'Rp0.2;Rp1.0;Rm;A5;A50;A80;'
reqs_l1= lines[0].rstrip()
reqs_l2= lines[1].rstrip()
reqs_l3= lines[2].rstrip()
reqs_l4= lines[3].rstrip()
grade = lines[4].rstrip()
size_1 = (lines[5].replace(" ", "")).rstrip()
size_2 = (lines[6].replace(" ", "")).rstrip()
size_3 = (lines[7].replace(" ", "")).rstrip()
size_4 = (lines[8].replace(" ", "")).rstrip()
size_5 = (lines[9].replace(" ", "")).rstrip()
#sample data heat1
sample1_1_rp02 = lines[16][6:10].rstrip()
sample1_1_rp10 = lines[16][11:15].rstrip()
sample1_1_rm = lines[16][16:20].rstrip()
sample1_1_a5 = lines[16][21:25].rstrip()
sample1_1_a50 = lines[16][26:30].rstrip()
sample1_1_a80 = lines[16][31:35].rstrip()
sample1_1_hardness = lines[16][36:40].rstrip()
sample2_1_rp02 = lines[17][6:10].rstrip()
sample2_1_rp10 = lines[17][11:15].rstrip()
sample2_1_rm = lines[17][16:20].rstrip()
sample2_1_a5 = lines[17][21:25].rstrip()
sample2_1_a50 = lines[17][26:30].rstrip()
sample2_1_a80 = lines[17][31:35].rstrip()
sample2_1_hardness = lines[17][36:40].rstrip()
sample1_1 = ((sample1_1_rp02+';'+sample1_1_rp10+';'+sample1_1_rm+';'+sample1_1_a5+';'+sample1_1_a50+';'+sample1_1_a80+';'+sample1_1_hardness+';').replace(" ", "")).rstrip()
sample2_1 = ((sample2_1_rp02+';'+sample2_1_rp10+';'+sample2_1_rm+';'+sample2_1_a5+';'+sample2_1_a50+';'+sample2_1_a80+';'+sample2_1_hardness+';').replace(" ", "")).rstrip()
chemical_comp_1 = (((lines[11][6:]).replace(" ",";")).rstrip())+";"
c_fields_count_1 = chemical_comp_1.count(';')
if c_fields_count_1 < 12:
missing_f = 12 - c_fields_count_1
chemical_comp_1 = chemical_comp_1+(missing_f * ";")
extra_c_fields_1 = (((lines[10]).replace(" ","%;")).rstrip())+"%;"
extra_c_fields_count_1 = extra_c_fields_1.count(';')
if extra_c_fields_count_1 < 5:
extra_missing_fields_1 = 5 - extra_c_fields_count_1
extra_c_fields_1 = extra_c_fields_1+(extra_missing_fields_1 * ";")
row_1 = cert_num+";"+date+";"+marking+";"+reqs_l1+";"+reqs_l2+";"+reqs_l3+";"+reqs_l4+";"+grade+";"+size_1+";"+weight_1+";"+heat_1+";"+line_flds+line_hardness_hdr+";"+sample1_1+sample2_1+"C%;Si%;Mn%;P%;S%;Cr%;Ni%;"+extra_c_fields_1+chemical_comp_1
#sample data heat1 end
#sample data heat2
sample1_2_rp02 = lines[18][6:10].rstrip()
sample1_2_rp10 = lines[18][11:15].rstrip()
sample1_2_rm = lines[18][16:20].rstrip()
sample1_2_a5 = lines[18][21:25].rstrip()
sample1_2_a50 = lines[18][26:30].rstrip()
sample1_2_a80 = lines[18][31:35].rstrip()
sample1_2_hardness = lines[18][36:40].rstrip()
sample2_2_rp02 = lines[19][6:10].rstrip()
sample2_2_rp10 = lines[19][11:15].rstrip()
sample2_2_rm = lines[19][16:20].rstrip()
sample2_2_a5 = lines[19][21:25].rstrip()
sample2_2_a50 = lines[19][26:30].rstrip()
sample2_2_a80 = lines[19][31:35].rstrip()
sample2_2_hardness = lines[19][36:40].rstrip()
sample1_2 = ((sample1_2_rp02+';'+sample1_2_rp10+';'+sample1_2_rm+';'+sample1_2_a5+';'+sample1_2_a50+';'+sample1_2_a80+';'+sample1_2_hardness+';').replace(" ", "")).rstrip()
sample2_2 = ((sample2_2_rp02+';'+sample2_2_rp10+';'+sample2_2_rm+';'+sample2_2_a5+';'+sample2_2_a50+';'+sample2_2_a80+';'+sample2_2_hardness+';').replace(" ", "")).rstrip()
chemical_comp_2 = (((lines[12][6:]).replace(" ",";")).rstrip())+";"
c_fields_count_2 = chemical_comp_2.count(';')
if c_fields_count_2 < 12:
missing_f = 12 - c_fields_count_2
chemical_comp_2 = chemical_comp_2+(missing_f * ";")
extra_c_fields_2 = (((lines[10]).replace(" ","%;")).rstrip())+"%;"
extra_c_fields_count_2 = extra_c_fields_2.count(';')
if extra_c_fields_count_2 < 5:
extra_missing_fields_2 = 5 - extra_c_fields_count_2
extra_c_fields_2 = extra_c_fields_2+(extra_missing_fields_2 * ";")
row_2 = cert_num+";"+date+";"+marking+";"+reqs_l1+";"+reqs_l2+";"+reqs_l3+";"+reqs_l4+";"+grade+";"+size_2+";"+weight_2+";"+heat_2+";"+line_flds+line_hardness_hdr+";"+sample1_2+sample2_2+"C%;Si%;Mn%;P%;S%;Cr%;Ni%;"+extra_c_fields_2+chemical_comp_2
#sample data heat2 end
#sample data heat3
sample1_3_rp02 = lines[20][6:10].rstrip()
sample1_3_rp10 = lines[20][11:15].rstrip()
sample1_3_rm = lines[20][16:20].rstrip()
sample1_3_a5 = lines[20][21:25].rstrip()
sample1_3_a50 = lines[20][26:30].rstrip()
sample1_3_a80 = lines[20][31:35].rstrip()
sample1_3_hardness = lines[20][36:40].rstrip()
sample2_3_rp02 = lines[21][6:10].rstrip()
sample2_3_rp10 = lines[21][11:15].rstrip()
sample2_3_rm = lines[21][16:20].rstrip()
sample2_3_a5 = lines[21][21:25].rstrip()
sample2_3_a50 = lines[21][26:30].rstrip()
sample2_3_a80 = lines[21][31:35].rstrip()
sample2_3_hardness = lines[21][36:40].rstrip()
sample1_3 = ((sample1_3_rp02+';'+sample1_3_rp10+';'+sample1_3_rm+';'+sample1_3_a5+';'+sample1_3_a50+';'+sample1_3_a80+';'+sample1_3_hardness+';').replace(" ", "")).rstrip()
sample2_3 = ((sample2_3_rp02+';'+sample2_3_rp10+';'+sample2_3_rm+';'+sample2_3_a5+';'+sample2_3_a50+';'+sample2_3_a80+';'+sample2_3_hardness+';').replace(" ", "")).rstrip()
chemical_comp_3 = (((lines[13][6:]).replace(" ",";")).rstrip())+";"
c_fields_count_3 = chemical_comp_3.count(';')
if c_fields_count_3 < 12:
missing_f = 12 - c_fields_count_3
chemical_comp_3 = chemical_comp_3+(missing_f * ";")
extra_c_fields_3 = (((lines[10]).replace(" ","%;")).rstrip())+"%;"
extra_c_fields_count_3 = extra_c_fields_3.count(';')
if extra_c_fields_count_3 < 5:
extra_missing_fields_3 = 5 - extra_c_fields_count_3
extra_c_fields_3 = extra_c_fields_3+(extra_missing_fields_3 * ";")
row_3 = cert_num+";"+date+";"+marking+";"+reqs_l1+";"+reqs_l2+";"+reqs_l3+";"+reqs_l4+";"+grade+";"+size_3+";"+weight_3+";"+heat_3+";"+line_flds+line_hardness_hdr+";"+sample1_3+sample2_3+"C%;Si%;Mn%;P%;S%;Cr%;Ni%;"+extra_c_fields_3+chemical_comp_3
#sample data heat3
#sample data heat4
sample1_4_rp02 = lines[22][6:10].rstrip()
sample1_4_rp10 = lines[22][11:15].rstrip()
sample1_4_rm = lines[22][16:20].rstrip()
sample1_4_a5 = lines[22][21:25].rstrip()
sample1_4_a50 = lines[22][26:30].rstrip()
sample1_4_a80 = lines[22][31:35].rstrip()
sample1_4_hardness = lines[22][36:40].rstrip()
sample2_4_rp02 = lines[23][6:10].rstrip()
sample2_4_rp10 = lines[23][11:15].rstrip()
sample2_4_rm = lines[23][16:20].rstrip()
sample2_4_a5 = lines[23][21:25].rstrip()
sample2_4_a50 = lines[23][26:30].rstrip()
sample2_4_a80 = lines[23][31:35].rstrip()
sample2_4_hardness = lines[23][36:40].rstrip()
sample1_4 = ((sample1_4_rp02+';'+sample1_4_rp10+';'+sample1_4_rm+';'+sample1_4_a5+';'+sample1_4_a50+';'+sample1_4_a80+';'+sample1_4_hardness+';').replace(" ", "")).rstrip()
sample2_4 = ((sample2_4_rp02+';'+sample2_4_rp10+';'+sample2_4_rm+';'+sample2_4_a5+';'+sample2_4_a50+';'+sample2_4_a80+';'+sample2_4_hardness+';').replace(" ", "")).rstrip()
chemical_comp_4 = (((lines[14][6:]).replace(" ",";")).rstrip())+";"
c_fields_count_4 = chemical_comp_4.count(';')
if c_fields_count_4 < 12:
missing_f = 12 - c_fields_count_4
chemical_comp_4 = chemical_comp_4+(missing_f * ";")
extra_c_fields_4 = (((lines[10]).replace(" ","%;")).rstrip())+"%;"
extra_c_fields_count_4 = extra_c_fields_4.count(';')
if extra_c_fields_count_4 < 5:
extra_missing_fields_4 = 5 - extra_c_fields_count_4
extra_c_fields_4 = extra_c_fields_4+(extra_missing_fields_4 * ";")
row_4 = cert_num+";"+date+";"+marking+";"+reqs_l1+";"+reqs_l2+";"+reqs_l3+";"+reqs_l4+";"+grade+";"+size_4+";"+weight_4+";"+heat_4+";"+line_flds+line_hardness_hdr+";"+sample1_4+sample2_4+"C%;Si%;Mn%;P%;S%;Cr%;Ni%;"+extra_c_fields_4+chemical_comp_4
#sample data heat4 end
#sample data heat5
sample1_5_rp02 = lines[24][6:10].rstrip()
sample1_5_rp10 = lines[24][11:15].rstrip()
sample1_5_rm = lines[24][16:20].rstrip()
sample1_5_a5 = lines[24][21:25].rstrip()
sample1_5_a50 = lines[24][26:30].rstrip()
sample1_5_a80 = lines[24][31:35].rstrip()
sample1_5_hardness = lines[24][36:40].rstrip()
sample2_5_rp02 = lines[25][6:10].rstrip()
sample2_5_rp10 = lines[25][11:15].rstrip()
sample2_5_rm = lines[25][16:20].rstrip()
sample2_5_a5 = lines[25][21:25].rstrip()
sample2_5_a50 = lines[25][26:30].rstrip()
sample2_5_a80 = lines[25][31:35].rstrip()
sample2_5_hardness = lines[25][36:40].rstrip()
sample1_5 = ((sample1_5_rp02+';'+sample1_5_rp10+';'+sample1_5_rm+';'+sample1_5_a5+';'+sample1_5_a50+';'+sample1_5_a80+';'+sample1_5_hardness+';').replace(" ", "")).rstrip()
sample2_5 = ((sample2_5_rp02+';'+sample2_5_rp10+';'+sample2_5_rm+';'+sample2_5_a5+';'+sample2_5_a50+';'+sample2_5_a80+';'+sample2_5_hardness+';').replace(" ", "")).rstrip()
chemical_comp_5 = (((lines[15][6:]).replace(" ",";")).rstrip())+";"
c_fields_count_5 = chemical_comp_5.count(';')
if c_fields_count_5 < 12:
missing_f = 12 - c_fields_count_5
chemical_comp_5 = chemical_comp_5+(missing_f * ";")
extra_c_fields_5 = (((lines[10]).replace(" ","%;")).rstrip())+"%;"
extra_c_fields_count_5 = extra_c_fields_5.count(';')
if extra_c_fields_count_5 < 5:
extra_missing_fields_5 = 5 - extra_c_fields_count_5
extra_c_fields_5 = extra_c_fields_5+(extra_missing_fields_5 * ";")
row_5 = cert_num+";"+date+";"+marking+";"+reqs_l1+";"+reqs_l2+";"+reqs_l3+";"+reqs_l4+";"+grade+";"+size_5+";"+weight_5+";"+heat_5+";"+line_flds+line_hardness_hdr+";"+sample1_5+sample2_5+"C%;Si%;Mn%;P%;S%;Cr%;Ni%;"+extra_c_fields_5+chemical_comp_5
#sample data heat5 end
export = open('out.csv', 'w')
export.write(row_1+'\n'+row_2+'\n'+row_3+'\n'+row_4+'\n'+row_5)
export.flush()
And this is the txt file to process:
EN 10088-4:2009
AD 2000 W2, W10 & EN 10028-7:2016
ASTM A240/A240M
ASME SA-240/SA-240M II A ED. 2021
AOD
COIL
1.4301 TYPE 304 EN ISO 9445-2
1.4301 2B
1 1 33564 3 0,6 X 1250 MM 14070 KG 2B
2 2 38026 1 0,6 X 1250 MM 11850 KG 2B
3 3 35800 2 0,7 X 1500 MM 11710 KG 2B
4 4 35800 2 0,7 X 1500 MM 11380 KG 2B
5 5 38426 5 0,8 X 1250 MM 13580 KG 2B
N
%
33564 0,04 0,43 1,34 0,033 0,002 18,2 8,6 0,042
38026 0,04 0,51 1,46 0,032 <.001 18,2 8,6 0,043
35800 0,036 0,48 1,45 0,035 0,001 18,2 8,7 0,045
38426 0,04 0,41 1,12 0,030 <.001 18,2 9,0 0,068
A50 A80
HBW
1 01 266 294 638 63 59 144
02 260 289 635 62 58 143
2 11 276 297 632 60 57 141
12 263 287 624 61 58 143
3 01 256 288 614 61 58 132
02 284 324 625 58 55 149
4 01 256 288 614 61 58 132
02 284 324 625 58 55 149
5 01 255 287 605 62 59 148
02 251 284 608 64 60 145
OK
OK
OK
Up until the code below the file is edited just fine, after that it reverts as before adding the line in the code before it....
#find duplicate heats<---
openFile = open(output, "r")
readFile = openFile.read()
print (readFile )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
