I am trying to write a function that can take every individual line in a txt file and multiply that line by 2 so that each integer in the text file is doubled. So far I was able to get the code to print. However, when I added the code (reading & reading_int) to convert the strings to integers the function is now not working. There are no errors in the code to tell me what I am doing wrong. I am not sure what is wrong with reading and reading_int that is making my function not work.
def mult_num3():
data=[]
w = open('file3.txt', 'r')
with w as f:
reading = f.read()
reading_int = [int(x) for x in reading.split()]
for line in f:
currentline = line[:-1]
data.append(currentline)
for i in data:
w.write(int(i)*2)
w.close()
file3.txt:
1
2
3
4
5
6
7
8
9
10
Desired output:
2
4
6
8
10
12
14
16
18
20
Problems with original code:
def mult_num3():
data=[]
w = open('file3.txt', 'r') # only opened for reading, not writing
with w as f:
reading = f.read() # reads whole file
reading_int = [int(x) for x in reading.split()] # unused variable
for line in f: # file is empty now
currentline = line[:-1] # not executed
data.append(currentline) # not executed
for i in data: # data is empty, so...
w.write(int(i)*2) # not executed, can't write an int if it did
# and file isn't writable.
w.close() # not necessary, 'with' will close it
Note that int() ignores leading and trailing whitespace so no need for .split() if only one number per line, and a format string (f-string) can format each line as needed by converting and doubling the value and adding a newline.
with open('file3.txt', 'r') as f:
data = [f'{int(line)*2}\n' for line in f]
with open('file3.txt', 'w') as f:
f.writelines(data)
I added a try except to check for not integer data. I dont konw your data. But maybe it helps you.
def mult_num3():
input = open('file3.txt', 'r')
output = open('script_out.txt', 'w')
with input as f:
for line in f:
for value in line.split():
try:
output.write(str(int(value) * 2) + " ")
except:
output.write(
"(" + str(value + ": is not an integer") + ") ")
output.write("\n")
output.close()
Related
here is what I got txt and open
txt file looks like
f = open('data.txt', 'r')
print(f.read())
the show['Cat\n','Dog\n','Cat\n','Dog\n'........]
output
But I would like to get this
['C\n','D\n','C\n','D\n'........]
First you'll want to open the file in read mode (r flag in open), then you can iterate through the file object with a for loop to read each line one at a time. Lastly, you want to access the first element of each line at index 0 to get the first letter.
first_letters = []
with open('data.txt', 'r') as f:
for line in f:
first_letters.append(line[0])
print(first_letters)
If you want to have the newline character still present in the string you can modify line 5 from above to:
first_letters.append(line[0] + '\n')
f = open("data.txt", "r")
for x in f:
print(x[0])
f.close()
im trying to get some code which will look into a text file and split the lines into 2 variable but when i run this i get the error
ValueError: not enough values to unpack (expected 2, got 1)
the code is:
x=0
f = open("scores.txt","a")
f.write("\n")
f.write("andrew:78")
f.write("\n")
f.write("karen:64")
f.close
f = open("scores.txt","r")
a,b = ("karen:67").split(":")
print (a)
print(b)
scores = [[],[]]
while 0 != 1 :
line=(f.readline(x))
a,b = (line).split(":")
scores[0] = a
scores[1]=b
thats the text file
thanks in advance
here line=(f.readline(x)) is something like this (["content of file"]) ie it is file content in a list which is inside a tuple .
you need to change it the size define in x from 0 to some value or dont use it. ie from line = (f.readline(x)) to line = f.readline() it will be a string now.
now you can use split operation on string object . change a, b = (line).split(':') to a, b = line.split(':')
so new code will be
# opening/cretaing a file and adding data
with open("scores.txt", 'a') as f:
f.write("\n")
f.write("andrew:78")
f.write("\n")
f.write("karen:64")
# creating a dictionary to save data of name and score respectively
scores = {'name':[], 'score':[]}
# again opening the file in read mode
with open("scores.txt", 'r') as f:
data = f.readlines() # reading all line in file at single time
for each_line in data:
if line.split(':'): # checking if each line has format <name>:<score> or not, if yes then add that data to scores dict
name, score = line.split(':')
scores['name'].append(name)
scores['score'].append(score)
# seeing the result
print(scores)
It'll print out an empty character '' before the newline '\n' - I think what you want is something like:
lines = f.read().splitlines()
#This will only get the value for one line
scores[0], scores[1] = lines[0].split(':')
#Or to get both lines:
i=0
for line in f.read().splitlines():
scores[i][0], scores[i][1] = line.split(':')
i += 1
Does this code achieve what you want?
with open("scores.txt", "a") as f:
f.write(
"\nandrew:78"
"\nkaren:64"
)
scores = []
with open("scores.txt", "r") as f:
lines = f.readlines()
for line in lines:
if ":" in line:
a, b = (line).split(":")
scores.append({a: b})
You were probably having an error because some lines (the blank ones) didn't have the ":" so the line was only one string which you were trying to unpack into 2 variables.
I want to find and replace floats with integers in several text files.
There is one float value per text file which I want to convert. It is always after a specific keyword and has to be multiplied by 10.000.
e.g. the float 1.5 should be turned into the integer 15.000
The other floats after 1.5 don't have to be changed though
def edit(file):
with open(file, 'r') as f:
filedata = f.read()
for line in filedata:
if "keyword" in line:
filedata = filedata.replace(re.search(r"\d+\.\d+", line).group(), str(10000*re.search(r"\d+\.\d+", line).group()))
with open(file, 'w') as f:
f.write(filedata)
I was trying to replace the the float using a regex. But this doesn't work
EXAMPLE FILE EXTRACT
abcdef 178 211 208 220
ghijkl 0 0 0 0
keyword 1.50 1.63 1.56 1.45
You can iterate over lines with lines = filedata.split("\n"). Be careful because filedata is a big string containing the whole file. When you did for line in filedata, you iterated over every character of the file...
I also used another way (without regex) to find numbers and change them.
def edit(file):
with open(file, "r") as f:
filedata = f.read()
lines = filedata.split("\n") # list of lines
for index, line in enumerate(lines):
if "keyword" in line:
words = line.split() # ['keyword', '1.50', '1.63', '1.56', '1.45']
for i, w in enumerate(words):
try:
# transform number to float, multiply by 10000
# then transform to integer, then back to string
new_word = str(int(float(w)*10000))
words[i] = new_word
except:
pass
lines[index] = " ".join(words)
new_data = "\n".join(lines) # store new data to overwrite file
with open(file, "w") as f: # open file with write permission
f.write(new_data) # overwrite the file with our modified data
edit("myfile.txt")
Output :
# myfile.txt
abcdef 178 211 208 220
ghijkl 0 0 0 0
keyword 15000 16299 15600 14500
EDIT : More Compact way
def edit(file):
with open(file, "r") as f:
filedata = f.read()
line = [x for x in filedata.split("\n") if "keyword" in x][0]
new_line = line
for word in line.split():
try: new_line = new_line.replace(word, str(int(float(word)*10000)))
except: pass
with open(file, "w") as f: # open file with write permission
f.write(filedata.replace(line, new_line)) # overwrite the file with our modified data
edit("myfile.txt")
When you find yourself using a regex inside a loop, you should compile it ouside of the loop.
Next, if you want to replace a value in a line, you should not search for it in the whole file.
Finally you must cast a string to a numeric type to operate on it. If you do not you will just repeat the string ('10' * 2 is '1010' not 20 nor '20')
Here is a possible improvement of your code:
def edit(file):
with open(file, 'r') as f:
rx = re.compile(r"\d+\.\d+") # compile the regex only once
filedata = f.readlines() # get a list of the lines of the file
for i, line in enumerate(filedata): # and enumerate them
if "keyword" in line:
val = re.search(r"\d+\.\d+", line).group() # split the complex line
newval = str(int(float(val) * 10000))
filedata[i] = line.replace(val, newval) # replace only the current line
break # no need to proceed further
with open(file, 'w') as f:
f.write(filedata)
I have a tab separated text file like these example:
infile:
chr1 + 1071396 1271396 LOC
chr12 + 1101483 1121483 MIR200B
I want to divide the difference between columns 3 and 4 in infile into 100 and make 100 rows per row in infile and make a new file named newfile
and make the final tab separated file with 6 columns. The first 5 columns would be like infile, the 6th column would be (5th column)_part number (number is 1 to 100).
This is the expected output file:
expected output:
chr1 + 1071396 1073396 LOC LOC_part1
chr1 + 1073396 1075396 LOC LOC_part2
.
.
.
chr1 + 1269396 1271396 LOC LOC_part100
chr12 + 1101483 1101683 MIR200B MIR200B_part1
chr12 + 1101683 1101883 MIR200B MIR200B_part2
.
.
.
chr12 + 1121283 1121483 MIR200B MIR200B_part100
I wrote the following code to get the expected output but it does not return what I expect.
file = open('infile.txt', 'rb')
cont = []
for line in file:
cont.append(line)
newfile = []
for i in cont:
percent = (i[3]-i[2])/100
for j in percent:
newfile.append(i[0], i[1], i[2], i[2]+percent, i[4], i[4]_'part'percent[j])
with open('output.txt', 'w') as f:
for i in newfile:
for j in i:
f.write(i + '\n')
Do you know how to fix the problem?
Try this:
file = open('infile.txt', 'rb')
cont = []
for line in file:
cont.append(list(filter(lambda x: not x.isspace(), line.split(' ')))
newfile = []
for i in cont:
diff= (int(i[3])-int(i[2]))/100
left = i[2]
right = i[2] + diff
for j in range(100):
newfile.append(i[0], i[1], left, right, i[4], i[4]_'part' + j)
left = right
right = right + diff
with open('output.txt', 'w') as f:
for i in newfile:
for j in i:
f.write(i + '\n')
In your code for i in cont youre loop over the string and i is a char and not string.
To fix that i split the line and remove spaces.
Here are some suggestions:
when you open the file, open it as a text file, not a binary file.
open('infile.txt','r')
now, when you read it line by line, you should strip the newline character at the end by using strip(). Then, you need to split your input text line by tabs into a list of strings, vs a just a long string containing your line, by using split('\t'):
line.strip().split('\t')
now you have:
file = open('infile.txt', 'r')
cont = []
for line in file:
cont.append(line.strip().split('\t))
now cont is a list of lists, where each list contains your tab separated data. i.e.
cont[1][0] = 'chr12'.
You will probably able to take it from here.
Others have answered your question with respect to your own code, I thought I would leave my attempt at solving your problem here.
import os
directory = "C:/Users/DELL/Desktop/"
filename = "infile.txt"
path = os.path.join(directory, filename)
with open(path, "r") as f_in, open(directory+"outfile.txt", "w") as f_out: #open input and output files
for line in f_in:
contents = line.rstrip().split("\t") #split line into words stored as a string 'contents'
diff = (int(contents[3]) - int(contents[2]))/100
for i in range(100):
temp = (f"{contents[0]}\t+\t{int(int(contents[2])+ diff*i)}\t{contents[3]}\t{contents[4]}\t{contents[4]}_part{i+1}")
f_out.write(temp+"\n")
This code doesn't follow python style convention well (excessively long lines, for example) but it works. The line temp = ... uses fstrings to format the output string conveniently, which you could read more about here.
I have textfiles that have the date stored on line 7 of each file, formatted as such:
Date: 1233PM 14 MAY 00
I would like to search through each file and get the new line 7 to be formatted as such:
Date: 1233PM 14 MAY 2000
So, basically, I just need to stick a '20' in front of the last two digits in line seven.
Probably not the most difficult problem, but I have been having difficulty as textfile.readlines() reads everything into the first (textfile[0]) position.
You can read all the file, change the specified line then save it again:
arc = open('file_name.txt').readlines()[0].split('\r')
#Do what you want with the 7th line i.e. arc[6]
new_arc = open('file_name.txt','w')
for line in arc:
new_arc.write(line)
new_arc.write('\n')
new_arc.close()
Maybe this:
with open(filename, 'r') as f:
lines = f.readlines()
with open(filename, 'w') as f:
for idx, line in lines:
if idx == 7: # or 6
vals = line.split()
if len(vals[-1]) == 2:
vals[-1] = '20'+vals[-1]
line = ' '.join(vals)
f.write(line)
Try this:
# open file
f = open("file.txt" , 'rU+b')
lines = f.readlines()
# modify line 7
lines[6] = lines[6][:-2] + "20" + lines[6][-2:]
# return file pointer to the top so we can rewrite the file
f.seek(0)
f.truncate()
# write the file with new content
f.write(''.join(lines))
f.close