How to delete several lines in txt with conditional - python

everyone! I have just started learning python
I have a problem with some txt file
I want to delete all data with KMAG less than 5.5
but I have no idea any suggestions?
code below just what I could
file = open("experiment.txt", "r")
for line in file:
if 'KMAG' in line:
print(line)
file.close()
enter image description here

You need to do two things. First, it appears that this file has multiline records delimited by a line with a single decimal number. Use that to read the file a record at a time:
import re
from decimal import Decimal
def get_records(fileobj):
record = []
for line in fileobj:
if re.match(r"\s*\d+\s*$", line):
# got new record, emit old
if record:
yield record
record = [line]
else:
record.append(line)
if record:
yield record
return
Now you can peek into each record to see if its data you want to keep. I'm using the decimal module because because the python binary float does not exactly represent decimal floats.
min_val = Decimal("5.5")
with open("experiment.txt") as infile, open("foo.txt", "w") as outfile:
for record in get_records(infile):
# we got record number\nheader\ndata with kmag\n...
kmag = re.split(r"\s+", record[2].strip())[-1]
if Decimal(kmag) >= min_val:
outfile.writelines(record)

Related

read a file and converts each decimal into a binary number

Reads the text file (the file name is given as a parameter) and converts each decimal number into a binary number. All decimal numbers are positive, so you do not need to account for the negative scenario. Print each converted binary number to the console, one per line.
(Create a module named binary that has each of the functions listed below. You will notice these functions are used in the main.py file, which is how you will test your code).
File dec2bin.txt
11
2090
103
58
9049
20012948
129129
291
2039193
1234872589
8717950
main.py
import binary as b
b.dec2bin("dec2bin.txt")
print()
what am I doing wrong?
my code is not working and I tried different coding.
def dec2bin(file):
with open(file, 'r') as f:
for line in f:
print(bin(int(line)).replace("0b", ""))
def bin2dec(file):
with open(file, 'r') as f:
for line in f:
print(int(line, 2))
# Inside binary.py file
def dec2bin(data):
lst = []
for a in data:
lst.append(bin(int(a))[2:])
return(lst)
# another file.
from binary import *
with open('test.txt') as f:
data = f.read().splitlines()
if __name__ == '__main__':
print(dec2bin(data))
OUTPUT
['1011', '100000101010', '1100111', '111010', '10001101011001', '1001100010101111110010100', '11111100001101001', '100100011', '111110001110110011001', '1001001100110101010100100001101', '100001010000011001111110']

Subtracting Numbers From A .txt File In Python

I want to be able to open the file i have, and append it so that if i want to subtract the number in the file by 2, it would print out the answer in the console by opening the file and reading it.
e.g. if the number in the file was 156, i would have to subtract it by 2, which is 154, this will then be displayed on the console!
this is all i have so far:
a = file.open("xp.txt", "r")
a.read()
a.close()
How would i update it so that if i wanted to subtract it by an integer, that integer would be displayed on console?
Thanks in advance!
Use readline instead of read so that you won't get an error when the file for example contains another empty line. Then, call strip on the result to eliminate possible whitespace. Finally, use int to convert the string to a number. Now you can do all the math you want with it:
with open("xp.txt", "r") as infile:
value = infile.readline()
stripped = value.strip()
number = int(stripped)
newNumber = number - 2
print(newNumber)
Or shorter:
with open("xp.txt", "r") as infile:
print(int(infile.readline().strip()) - 2)
To write the number to the same file, convert the number back to a string:
with open("xp.txt", "r") as infile:
result = int(infile.readline().strip()) - 2
print(result)
with open("xp.txt" , "w") as outfile:
outfile.write(str(result))
Assuming the file just contained that single value and nothing else, you could accomplish this using the following
with open('xp.txt', 'r') as f_in:
value = int(a.read())
value -= 2
print(f'New value is {value}')
with open('xp.txt', 'w') as f_out:
f_out.write(str(value))
Basically you open the file for reading, read the value into an integer, modify the value and display it, then re-open the file for writing to write the value back out.

Calculate the average value of the numbers in a file

I am having a problem of calculating the average value of numbers in a file.
So far i have made a function that reads in files and calculate the number of lines.
The file consists of many columns of numbers, but the column 8 is the one i need to calculate from.
def file_read():
fname = input("Input filname: ")
infile = open(fname,'r')
txt = infile.readlines()
print("opens",fname,"...")
num_lines = sum(1 for line in open(fname))
#The first line in the file is only text, so i subtract 1
print("Number of days:",(num_lines-1))
The numbers are also decimals, so i use float.
This is my try on calculating the sum of numbers,
which shall be divided by the number of lines , but i comes an error, cuz the first line is text.
with open(fname) as txt:
return sum(float(x)
for line in txt
for x in line.split()[8]
Is there a way i can get python to ignore the first line and just concentrate about the numbers down under?
You could use txt.readline() to read the first line, but to stick with iterators way to do it, just drop the first line using iteration on file with next
with open(fname) as txt:
next(txt) # it returns the first line, we just ignore the return value
# your iterator is now on the second line, where the numbers are
for line in txt:
...
Side note: this is also very useful to skip title lines of files open with the csv module, that's where next is better than readline since csv title can be on multiple lines.
Try this
import re
#regular expression for decimals
digits_reg = re.compile(r"\d+\.\d+|\d+")
with open('''file name''', "r") as file:
allNum = []
#find numbers in each line and add them to the list
for line in file:
allNum.extend(digits_reg.findall(line))
#should be a list that contains all numbers in the file
print(alNum)

Printing to a file via Python

Hopefully this is an easy fix. I'm trying to edit one field of a file we use for import, however when I run the following code it leaves the file blank and 0kb. Could anyone advise what I'm doing wrong?
import re #import regex so we can use the commands
name = raw_input("Enter filename:") #prompt for file name, press enter to just open test.nhi
if len(name) < 1 : name = "test.nhi"
count = 0
fhand = open(name, 'w+')
for line in fhand:
words = line.split(',') #obtain individual words by using split
words[34] = re.sub(r'\D', "", words[34]) #remove non-numeric chars from string using regex
if len(words[34]) < 1 : continue # If the 34th field is blank go to the next line
elif len(words[34]) == 2 : "{0:0>3}".format([words[34]]) #Add leading zeroes depending on the length of the field
elif len(words[34]) == 3 : "{0:0>2}".format([words[34]])
elif len(words[34]) == 4 : "{0:0>1}".format([words[34]])
fhand.write(words) #write the line
fhand.close() # Close the file after the loop ends
I have taken below text in 'a.txt' as input and modified your code. Please check if it's work for you.
#Intial Content of a.txt
This,program,is,Java,program
This,program,is,12Python,programs
Modified code as follow:
import re
#Reading from file and updating values
fhand = open('a.txt', 'r')
tmp_list=[]
for line in fhand:
#Split line using ','
words = line.split(',')
#Remove non-numeric chars from 34th string using regex
words[3] = re.sub(r'\D', "", words[3])
#Update the 3rd string
# If the 3rd field is blank go to the next line
if len(words[3]) < 1 :
#Removed continue it from here we need to reconstruct the original line and write it to file
print "Field empty.Continue..."
elif len(words[3]) >= 1 and len(words[3]) < 5 :
#format won't add leading zeros. zfill(5) will add required number of leading zeros depending on the length of word[3].
words[3]=words[3].zfill(5)
#After updating 3rd value in words list, again creating a line out of it.
tmp_str = ",".join(words)
tmp_list.append(tmp_str)
fhand.close()
#Writing to same file
whand = open("a.txt",'w')
for val in tmp_list:
whand.write(val)
whand.close()
File content after running code
This,program,is,,program
This,program,is,00012,programs
The file mode 'w+' Truncates your file to 0 bytes, so you'll only be able to read lines that you've written.
Look at Confused by python file mode "w+" for more information.
An idea would be to read the whole file first, close it, and re-open it to write files in it.
Not sure which OS you're on but I think reading and writing to the same file has undefined behaviour.
I guess internally the file object holds the position (try fhand.tell() to see where it is). You could probably adjust it back and forth as you went using fhand.seek(last_read_position) but really that's asking for trouble.
Also, I'm not sure how the script would ever end as it would end up reading the stuff it had just written (in a sort of infinite loop).
Best bet is to read the entire file first:
with open(name, 'r') as f:
lines = f.read().splitlines()
with open(name, 'w') as f:
for l in lines:
# ....
f.write(something)
For 'Printing to a file via Python' you can use:
ifile = open("test.txt","r")
print("Some text...", file = ifile)

what is a quick way to import a text file in python?

I have a plain text file with a sequence of numbers, one on each line. I need to import those values into a list. I'm currently learning python and I'm not sure of which is a fast or even "standard" way of doing this (also, I come from R so I'm used to the scan or readLines functions that makes this task a breeze).
The file looks like this (note: this isn't a csv file, commas are decimal points):
204,00
10,00
10,00
10,00
10,00
11,00
70,00
276,00
58,00
...
Since it uses commas instead of '.' for decimal points, I guess the task's a little harder, but it should be more or less the same, right?
This is my current solution, which I find quite cumbersome:
f = open("some_file", "r")
data = f.read().replace('\n', '|')
data = data[0:(len(data) - 2)].replace(',', '.')
data = data.split('|')
x = range(len(data))
for i in range(len(data)):
x[i] = float(data[i])
Thanks in advance.
UPDATE
I didn't realize the comma was the decimal separator. If the locale is set right, something like this should work
lines = [locale.atof(line.strip()) for line in open(filename)]
if not, you could do
lines = [float(line.strip().replace(',','.')) for line in open(filename)]
lines = [line.strip() for line in open(filename)]
if you want the data as numbers ...
lines = [map(float,line.strip().split(',')) for line in open(filename)]
edited as per first two comments below
bsoist's answer is good if locale is set correctly. If not, you can simply read the entire file in and split on the line breaks (\n), then use a list comprehension for replacements.
with open('some_file.txt', 'r') as datafile:
data = datafile.read()
x = [float(value.replace(",", ".")) for value in data.split('\n')]
For a more simpler way you could just do
Read = []
with open('File.txt', 'r') as File:
Read = File.readLines()
for A in Read:
print A
The "with open()" will open the file and quit when it's finished reading. This is good practice IIRC.
Then the For loop will just loop over Read and print out the lines.

Categories

Resources