I want to read the input file, line by line, then modify one line and write back the changes to the same file
the problem is that, after writing back, I lose the return to the line and I have all the data in one line
open(bludescFilePath, 'a+') as blu:
blu_file_in_lines = blu.readlines()
for line in blu_file_in_lines:
if "Length" in line:
blu_file_in_lines[13] = line.replace("0x8000",str(size))
with open(bludescFilePath, 'w') as blu:
blu.write(str(blu_file_in_lines))
EDIT
Ok, what was missing is the for loop.
with open(bludescFilePath, 'w') as blu:
for line in blu_file_in_lines:
blu.write(str(line))
Related
I tried to write a code that removes any line from a file that starts with a number smaller than T and which then writes the remaining lines to another file.
def filter(In,Out, T):
with open(In,'r') as In:
with open(Out,'r') as Out:
lines=In.readlines()
lines=[[e for e in line.split()] for line in lines]
lines=[line for line in lines if int(line[0])>=T]
for line in lines:
for word in line:
Out.write(f"{word} ")
return None
I thought The code would probably write the words in one long line instead of putting it per line but it just returned UnsupportedOperation: not writable and I don't understand why.
it seems like your code had a few bugs
you opened the file for reading with the line with open(Out,'r') as Out: and then tried to write to the file, so I changed the r to w
you tried to open the file for writing when it was already open for reading (you cant open a file while it's open) so i moved the code the writes back to the file to be after you have finished reading from it
you open the file for reading and gave it the name In but this name is already the name of an argument that you function is getting
this should do the trick:
def filter(In_name,Out, T):
with open(In_name,'r') as In:
lines=In.readlines()
lines=[[e for e in line.split()] for line in lines]
lines=[line for line in lines if int(line[0])>=T]
with open(Out, 'w') as Out:
for line in lines:
for word in line:
Out.write(f"{word} ")
return None
I have a very big txt file with uneven data lines. I want to delete a specific line from the txt file, but all methods shown in google shows to
Read the file to get all lines
Delete the specific line
Write to the file again
I want to know if there is any method in Python that I can use to delete the line from text file without having to write again.
#get list of lines.
a_file = open("sample.txt", "r")
lines = a_file. readlines()
a_file. close()
#remove line from list
del lines[1]
#write new contents into file
new_file = open("sample.txt", "w+")
for line in lines:
new_file. write(line)
new_file. close()
In python how we can select second line string of a text file.
I have a text file and in that file there are some data in line by line_
Dog
Cat
Cow
How to fetch the second line which is “Cat” and store that string in a variable.
f = open(“text_file.txt”, “r”)
var = # “Cat”
Can I use single line code without using any loop or anything like we open a file using this single line of code f = open(“text_file.txt”, “r”)
I'd suggest the following
with open('anim.txt','r') as f:
var = f.readlines()[1].strip()
print(var)
[1] get the second line
.strip() removes the new line character from the end of the string
with open... safely opens/closes the file
Open your file in this mode:
f = open('x.txt','r+',encoding='utf-8')
You can then read the files line by line:
f.readlines()
I am trying to encrypt 1 file and save the encrypted text from the first file into another file. I have it all working besides that it is only writing 1 line into the new file instead of the entire file.
file1 = open("passwords-plainText", "r")
for line in file1:
file2 = open("encryptedfile.txt", "w")
file2.write(encryptVignere(keyGen(), line))
The example file I am using looks like this
example
secondexample
new line
this is another new line
The output into the new file I am saving to only writes the first line and not the rest of the lines
ie.)
tyawakud
The file should look like this instead...
tyawakud
tqiibwaeeonozp
pttzucfqs
foxnzgjwtmbhnpwhjnapmsfg
You should only open file2 once:
file1 = open("passwords-plainText", "r")
file2 = open("encryptedfile.txt", "w")
for line in file1:
file2.write(encryptVignere(keyGen(), line))
Otherwise you just keep overwriting it.
i want to read my hl7 messages from a file line by line and parse them using python. I'm able to read But my problem is in parsing. It parses only my 1st line of the file and prints till the 2nd line but does not parses futhur because it tells that my 2nd line is not hl7. And the error shown is
h=hl7.parse(line)
File "C:\Python27\lib\site-packages\hl7\parser.py", line 45, in parse
plan = create_parse_plan(strmsg, factory)
File "C:\Python27\lib\site-packages\hl7\parser.py", line 88, in create_parse_plan
assert strmsg[:3] in ('MSH')
AssertionError
for the code:
with open('example.txt','r') as f:
for line in f:
print line
print hl7.isfile(line)
h=hl7.parse(line)
So how do i make my file a valid one. This is example.txt file
MSH|^~\&|AcmeMed|Lab|Main HIS|St.Micheals|20130408031655||ADT^A01|6306E85542000679F11EEA93EE38C18813E1C63CB09673815639B8AD55D6775|P|2.6|
EVN||20050622101634||||20110505110517|
PID|||231331||Garland^Tracy||19010201|F||EU|147 Yonge St.^^LA^CA^58818|||||||28-457-773|291-697-644|
NK1|1|Smith^Sabrina|Second Cousin|
NK1|2|Fitzgerald^Sabrina|Second Cousin|
NK1|3|WHITE^Tracy|Second Cousin|
OBX|||WT^WEIGHT||78|pounds|
OBX|||HT^HEIGHT||57|cm|
I had a similar issue and came up with this solution that works for me.
In short, put all your lines into an object and then parse that object.
(Obviously you can clean up the way I checked to see if the object is made yet or not, but I was going for an easy to read example.)
a = 0
with open('example.txt','r') as f:
for line in f:
if a == 0:
message = line
a = 1
else:
message += line
h=hl7.parse(message)
Now you will have to clean up some \r\n depending on how the file is encoded for end of the line values. But it takes the message as valid and you can parse to your hearts content.
for line in h:
print(line)
MSH|^~\&|AcmeMed|Lab|Main HIS|St.Micheals|20130408031655||ADT^A01|6306E85542000679F11EEA93EE38C18813E1C63CB09673815639B8AD55D6775|P|2.6|
EVN||20050622101634||||20110505110517|
PID|||231331||Garland^Tracy||19010201|F||EU|147 Yonge St.^^LA^CA^58818|||||||28-457-773|291-697-644|
NK1|1|Smith^Sabrina|Second Cousin|
NK1|2|Fitzgerald^Sabrina|Second Cousin|
NK1|3|WHITE^Tracy|Second Cousin|
OBX|||WT^WEIGHT||78|pounds|
OBX|||HT^HEIGHT||57|cm|
Tagging onto #jtweeder answer, the following code worked for prepping my HL7 data.
In notepad++, I noticed that each line ended with LF, but did not have CR. It seems as though this hl7 library requires \r, not \n.
filename = "TEST.dat"
lines = open(filepath + filename, "r").readlines()
h = '\r'.join(lines)