i have a .txt file with this(it should be random names, tho):
My Name 4 8 7
Your Name 5 8 7
You U 5 9 7
My My 4 8 5
Y Y 8 7 9
I need to put the information into text file results.txt with the names + average of the numbers. How do I do that?
with open(r'stuff.txt') as f:
mylist = list(f)
i = 0
sk = len(mylist)
while i < sk - 4:
print(mylist[i], mylist[i+1], mylist[i+2], mylist[i+3])
i = i + 3
Firstly, open both the input and output files:
with open("stuff.txt") as in_file:
with open("results.txt", "w") as out_file:
Since the problem only needs to work on each line independently, a simple loop over each line would suffice:
for line in in_file:
Split each line at the whitespaces into list of strings (row):
row = line.split()
The numbers occur after the first two fields:
str_nums = row[2:]
However, these are still strings, so they must be converted to a floating-point number to allow arithmetic to be performed on them. This results in a list of floats (nums):
nums = map(float, str_nums)
Now calculate the average:
avg = sum(nums) / len(str_nums)
Finally, write the names and the average into the output file.
out_file.write("{} {} {}\n".format(row[0], row[1], avg))
what about this?
with open(fname) as f:
new_lines = []
lines = f.readlines()
for each in lines:
col = each.split()
l = len(col)#<-- length of each line
average = (int(col[l-1])+int(col[l-2])+int(col[l-3]))/3
new_lines.append(col[0]+col[1]+str(average) + '\n')
for each in new_lines:#rewriting new lines into file
f.write(each)
f.close()
I tried, and this worked:
inputtxt=open("stuff.txt", "r")
outputtxt=open("output.txt", "w")
output=""""""
for i in inputtxt.readlines():
nums=[]
name=""
for k in i:
try:
nums.append(int(k))
except:
if k!=" ":
name+=k
avrg=0
for j in nums:
avrg+=j
avrg/=len(nums)
line=name+" "+str(avrg)+"\n"
output+=line
outputtxt.write(output)
inputtxt.close()
outputtxt.close()
Related
So basically I have a file with 3 lines and each line has 3 numbers
7,2,1
10,0,0
2,8,0
Then depending on the user input I want to +1 to one of the numbers on the line.
if user_input == 1
+1 to line1Number1
elif user_input == 2
+1 to line1Number2
elif user_input == 3
+1 to line1Number3
else
print"error"
You can do something like this:
In [1634]: user_input = int(input())
In [1627]: with open('t.txt', 'r') as f:
...: lines = f.readlines()
...: for c,l in enumerate(lines):
...: if c == user_input:
...: lst = l.split(',')
...: lst = [int(x) + 1 for x in lst]
...: print(lst)
[3, 9, 1]
with open(filename, "r") as txtr:
data = txtr.readlines()
data = [x.split(",") for x in data]
for i in range(len(data)):
for j in range(len(data[i])):
data[i][j] = int(data[i][j])
data now has 3 lists with 3 numbers each.
if user_input == 1
data[0] = [x+1 for x in data[0]]
just do the same for the rest.
to save to text file:
ndata = [",".join(x) for x in data]
nndata = "\n".join(ndata)
with open(filename, "w") as txtw:
txtw.write(nndata)
Another way of doing this (explanation in comment). Reads from in.txt, write to out.txt.
# ask user input for which column to update
update_column = int(input("Column to update 1,2 or 3?"))
# open file to read from
with open("in.txt", "r") as f:
# for every line in the text file
for line in f.readlines():
# make the numbers into an integer list (remove new line, split by comma, and convert to int)
new_line = list(map(int, line.strip().split(",")))
# add one to the number in the column input
new_line[update_column -1] +=1
# open a file for a pending
with open("out.txt", "a") as of:
# append the list removing brackets and adding a new line
of.write(str(new_line).replace("[","").replace("]","") + "\n")
I have a list of proxies separated by line. These proxies need to be separate into separate lists with sizes that I choose.
So I want the program to input how many lists of 10, 25, and 50 I need them to be split into, then output the new lists as a text file. The same proxy cannot be present in two separate lists.
This is what I've got so far simply to count the proxies
filename = input('Enter a file name: ')
with open(filename) as f:
line_count = 0
for line in f:
line_count += 1
print("Number of proxies: " + str(line_count))
Any tips on how to proceed?
You can achieve that by something like that:
def split_list(filename, size)
new_content = []
with open(filename) as f:
content = f.readlines()
for chunk in range(0, len(content), size):
new_content.append(content[chunk:chunk+size])
The code will generate numbers (range) from 0 to the length of the file. Using step param of range, we can increase the starting point by size every iteration.
The code will go through the list, and use slicing to get chunks of elements form the list constructing a new one. Those new lists will be the elements of a new list, new_content.
For a variable sizes try this:
def split_list(filename, sizes):
with open(filename) as f:
content = f.readlines()
new_content = []
start = 0
for size in sizes:
stop = start + size
new_content.append(content[start:stop])
start += size
return new_content
splitted_list = split_list('data.txt', [5, 2, 3])
for i, l in enumerate(splitted_list):
with open('{}.txt'.format(i), 'w') as f:
f.writelines(l)
Given data.txt is
1
2
3
4
5
6
7
8
9
10
it will generate three files (as specified in the second argument of the split_list function):
0.txt with the first 5 lines (the first specified chunk):
1
2
3
4
5
1.txt with the following 2 lines (the second chunk):
6
7
Finally 2.txt with the last 3 lines (third chunk):
8
9
10
I have to input a text file that contains comma seperated and line seperated data in the following format:
A002,R051,02-00-00,05-21-11,00:00:00,REGULAR,003169391,001097585,05-21-11,04:00:00,REGULAR,003169415,001097588,05-21-11,08:00:00,REGULAR,003169431,001097607
Multiple sets of such data is present in the text file
I need to print all this in new lines with the condition:
1st 3 elements of every set followed by 5 parameters in a new line. So solution of the above set would be:
A002,R051,02-00-00,05-21-11,00:00:00,REGULAR,003169391,001097585
A002,R051,02-00-00,05-21-11,04:00:00,REGULAR,003169415,001097588
A002,R051,02-00-00,05-21-11,08:00:00,REGULAR,003169431,001097607
My function to achieve it is given below:
def fix_turnstile_data(filenames):
for name in filenames:
f_in = open(name, 'r')
reader_in = csv.reader(f_in, delimiter = ',')
f_out = open('updated_' + name, 'w')
writer_out = csv.writer(f_out, delimiter = ',')
array=[]
for line in reader_in:
i = 0
j = -1
while i < len(line):
if i % 8 == 0:
i+=2
j+=1
del array[:]
array.append(line[0])
array.append(line[1])
array.append(line[2])
elif (i+1) % 8 == 0:
array.append(line[i-3*j])
writer_out.writerow(array)
else:
array.append(line[i-3*j])
i+=1
f_in.close()
f_out.close()
The output is wrong and there is a space of 3 lines at the end of those lines whose length is 8. I suspect it might be the writer_out.writerow(array) which is to blame.
Can anyone please help me out?
Hmm, the logic you use ends up being fairly confusing. I'd do it more along these lines (this replaces your for loop), and this is more Pythonic:
for line in reader_in:
header = line[:3]
for i in xrange(3, len(line), 5):
writer_out.writerow(header + line[i:i+5])
Basically I want to use python to read each data in the last two lines of the following file to a different variable.
The file is of the following form:
a b c
10
10 0 0
2 5
xyz
10 12 13
11 12 12.4
1 34.5 10.8
I want the output to have the following
d=11, e=12, f=12.4
g=1 h =34.5 i=10.8
How can I loop over the lines if I have say 100 lines (after xyz) each with three data. And that I need to read only say last 3 lines in it.
The following is what I did, but doesn't seem to reach anywhere.
p1=open('aaa','r')
im=open('bbb','w')
t=open('test','w')
lines=p1.readlines()
i=0
for line in lines:
Nj=[]
Nk=[]
Cx=Cy=Cz=Nx=Ny=Nz=0
i=i+1
if line.strip():
if i==1:
t.write(line)
dummy=line.strip().split()
a1=dummy[0]
a2=dummy[1]
a3=dummy[2]
print("The atoms present are %s, %s and %s" %(a1, a2,a3))
if i==2:
t.write(line)
if i==3:
t.write(line)
if i==4:
t.write(line)
if i==5:
t.write(line)
if i==6:
t.write(line)
dummy=line.strip().split()
Na1=dummy[0]
Na2=dummy[1]
Na3=dummy[2]
import string
N1=string.atoi(Na1)
N2=string.atoi(Na2)
N3=string.atoi(Na3)
print("number of %s atoms= %d "%(a1,N1))
print("number of %s atoms= %d "%(a2,N2))
print("number of %s atoms= %d "%(a3,N3))
if i==7:
t.write(line)
if i==8:
t.write(line)
for i, line in enumerate(p1):
if i==8:
dummy=line.strip().split()
Njx=dummy[0]
Njy=dummy[1]
Njz=dummy[2]
import string
Njx=string.atof(Njx)
Njy=string.atof(Njy)
Njz=string.atof(Njz)
Nj = [Njx, Njy, Njz]
elif i==9:
dummy=line.strip().split()
Nkx=dummy[0]
Nky=dummy[1]
Nkz=dummy[2]
import string
Nkx=string.atof(Nkx)
Nky=string.atof(Nky)
Nkz=string.atof(Nkz)
Nk = [Nkx, Nky, Nkz]
break
You can read the file's last two lines with
f = open(file, "r")
lines = f.readlines()[-2:] # change this if you want more than the last two lines
f.close()
split1 = lines[0].strip().split(' ') # In the example below: lines[0] = "4 5 6\n"
split2 = lines[1].strip().split(' ') # lines[1] = "7 8 9"
Then, you can assign those values to your variables:
d,e,f = [int(x) for x in split1]
g,h,i = [int(x) for x in split2]
This will assign the three values of each line to d,e,f,g,h,i, for example:
(your file)
...
1 2 3
4 5 6
7 8 9
(result)
d = 4
e = 5
f = 6
g = 7
h = 8
i = 9
Here you go
with open("text.txt", "r") as f:
# Get two last lines, remove the '\n'
contents = map(lambda s : s[:-1], f.readlines()[-2:])
# Get the three last lines,
[[d,e,f],[g,h,i]] = map(lambda s : map(float, s.split(" ")[-3:]), contents)
# Check the result
print (d,e,f,g,h,i)
Explanation :
with open("text.txt", "r") as f: is recommended way of working with file in python, see file I/O tutorial to see why.
contents = map(lambda s : s[:-1], f.readlines()[-2:]) This load the contents of f into a list of strings using readlines(), take the last two using [-2:], and remove the unnecessary '\n' by mapping lambda s : s[:-1].
At this point, our contents should contain last two lines.
The expression map(lambda s : map(float, s.split(" ")[-3:]), contents) split each of the two lines by " " then unpack it to the list [[d,e,f],[g,h,i]]. The [-3:] here is to remove the spaces in the front.
i need to print out average height from a .txt file. How do I write it in an easy way? The .txt file has these numbers:
12
14
59
48
45
12
47
65
152
this is what i've got so far:
import math
text = open(r'stuff.txt').read()
data = []
with open(r'stuff.txt') as f:
for line in f:
fields = line.split()
rowdata = map(float, fields)
data.extend(rowdata)
biggest = min(data)
smallest = max(data)
print(biggest - smallest)
To compute the average of some numbers, you should sum them up and then divide by the number of numbers:
data = []
with open(r'stuff.txt') as f:
for line in f:
fields = line.split()
rowdata = map(float, fields)
data.extend(rowdata)
print(sum(data)/len(data))
# import math -- you don't need this
# text = open(r'stuff.txt').read() not needed.
# data = [] not needed
with open(r'stuff.txt') as f:
data = [float(line.rstrip()) for line in f]
biggest = min(data)
smallest = max(data)
print(biggest - smallest)
print(sum(data)/len(data))
data = [float(ln.rstrip()) for ln in f.readlines()] # Within 'with' statement.
mean_average = float(sum(data))/len(data) if len(data) > 0 else float('nan')
That is the way to calculate the mean average, if that is what you meant. Sadly, math does not have a function for this. FYI, the mean_average line is modified in order to avoid the ZeroDivisionError bug that would occur if the list had a length of 0- just in case.
Array average can be computed like this:
print(sum(data) / len(data))
A simple program for finding the average would be the following (if I understand well, your file has one value in each line, if so, it has to be similar to this, else it has to change accordingly):
import sys
f = open('stuff.txt', 'rU')
lines = f.readlines()
f.close()
size = len(lines)
sum=0
for line in lines:
sum = sum + float(line.rstrip())
avg = sum / float(size)
print avg,
Not the best that can be in python but it's quite straight forward I think...
A full, almost-loopless solution combining elements of other answers here:
with open('stuff.txt','r') as f:
data = [float(line.rstrip()) for line in f.readlines()]
f.close()
mean = float(sum(data))/len(data) if len(data) > 0 else float('nan')
and you don't need to prepend, append, enclose or import anything else.