Python: writing a data file with three columns of integers - python

The code I am working with is:
fout = open('expenses.0.col', 'w')
for line in lines:
words = line.split()
amount = amountPaid(words)
num = nameMonth(words)
day = numberDay(words)
line1 = amount, num, day
fout.write(line1)
fout.close()
There is a file that you cannot see that the line in lines is pulling from which runs just fine. There are 100 lines within lines. When writing this last bit of code the goal is to get 100 lines of three columns which consist of the values: amount, num, and day. All three of these values are integers.
I have seen similar questions asked such as [python]Writing a data file using numbers 1-10, and I get the same error as that example. My problem is applying dataFile.write("%s\n" % line) to my case with three numbers in each line. Should be a quick 1 line of code fix.

In your example, line1 is a tuple - of numbers (I assume the functions amountPaid(), nameMonth(), numberDay() all return an integer or float).
You could do one of two things:
have these functions return the numbers as strings values
or cast the return values as string i.e.: amount =
str(amountPaid(words))
Once these values are strings you can simply do:
line1 = amount, num, day, '\n'
fout.write(''.join(line1))
Hope that helps!

use the print statement/function rather than the write method.

line1 = amount, num, day
fout.write("{}\n".format("".join(str(x) for x in line1)))

Related

Adding a comma to the end of every row in python

I have a list of sample codes which I input into a website to get information about each of them (they are codes for stars, but it doesn't matter what the codes are, they are just a long string of numbers). All these numbers are in one column, one number per row. The website I need to input this file into accepts the numbers to still be in a column, but with a comma next to the numbers. This is an example:
Instead of:
164891738509173
184818483848283
18483943491u385
It's supposed to look like this:
164891738509173,
184818483848283,
18483943491u385,
I wanted to program a quick python code to do that automatically for each number in the entire column. How do I do that? I can manage theoretically to do that manually if the number of stars I'm dealing with is little, but unfortunately in the website, I need to input something like 60000 stars (so 60000 of these numbers) so doing it manually is suicide.
Very simple:
open('output.txt', 'w').writelines( # open 'output.txt' for writing and write multiple lines
line.rstrip('\n') + ',\n' # append comma to each line
for line in open('input.txt') # read lines with numbers from 'input.txt'
)
You could do it more idiomatically and use a with block, but that's probably overkill for such a small task:
with open('input.txt') as In, open('output.txt', 'w') as Out:
for line in In:
Out.write(line.rstrip('\n') + ',\n')
Is this what you want?
If you want to add comma at end the every entry during printing, you can do this:
>>> codes = ['164891738509173', '184818483848283', '18483943491u385']
>>> for code in codes:
... print(code, end=',\n')
...
164891738509173,
184818483848283,
18483943491u385,
To add a comma to every item within the list,
>>> end_comma = [f"{code}," for code in codes]
>>> end_comma
['164891738509173,', '184818483848283,', '18483943491u385,']

Reading a numerical data file and assigning to each items at the same time

I am a beginner in python, and I have a problem when I want to read my numeric data file that contains more lines. My data in the input file contains rows that include a counter number, three float numbers, and finally, a character letter that all of them separated by space.it look like this:
1 12344567.143 12345678.154 1234.123 w
2 23456789.231 23413456.342 4321.321 f
I want to assign each item in the line to a specific parameter that I can use them to other steps.
like this "NO"=first item "X"=second item "Y"=third item "code"=forth item
I am trying to write it as follow:
f1=open('t1.txt','r')
line: float
for line in f1:
print(line.split(', ',4))
f1=float
select(line.split('')
nob: object(1)=int(line[1, 1])
cnt = 0
print(nob)
cnt +=1
but received more error each time when I run the program. Anyone can help me?
The error is probably due to the wrong indentation: in Python indentation is part of the syntax. It would be helpful if you also included the error message in your question.
How about this:
all_first_numbers = []
with open('t1.txt', 'r') as f:
for line in f:
values = line.split()
first_number = int(values[0])
second_number = float(values[1])
letter_code = values[4]
# If you want to save all the first numbers in one array:
all_first_numbers.append(first_number)

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)

python: findimg maximum number in first column of a file and considering other numbers on the same line in different columns

I have a very long file (about 2*10^5 rows times 5 columns) filled with numbers (floats).
I have to find the maximum value among the numbers of the first column and then consider the corresponding numbers on the other four columns on the same line.
I thought I might use use a dictionary: the keys are the number in the first column, the values are a list containing the others. I find the maximum among the keys and read the corresponding value.
Is there a smarter way? That dictionary is going to be very big...
I, almost forgot: I use python 2.6.
# define a sorting function based on the first numer, assuimg columns are
# separated by space or tab
f = lambda line: float(line.split()[0])
# opened file in Python is an iterator, so could be served to max() directly
with open('your_input_file') as inf:
line_with_max_num = max(inf, key=f)
# turn the other four numbers into a list and print them to the screen
# or do whatever you like with them
print [float(_) for _ in line_with_max_num.split()[1:]]
maxn=-float('inf')
with open(fname) as f:
for line in f:
if maxn<int(line.split(',')[0]):
theLine=line
#do something with that line:
print theLine
INPUT = "myfile.txt"
DELIM = ","
def first_float(s):
first = s.split(DELIM, 1)[0]
return float(first)
with open(INPUT) as inf:
max_line = max(inf, key=first_float)
max_data = [float(f) for f in max_line.split(DELIM)]

Python String Formatting, need to continuously add to a string

I just started python, any suggestions would be greatly appreciated. I'm reading a table generated by another program and pulling out 2 numbers from each line, I'll call them a and b. (they are saved as flux and observed in my program) I need to take these two numbers from each line and format them like this-
(a,b),(a,b),(a,b) ect.
Each consecutive parenthesis is from the consecutive line, first a,b is from line 1, second a,b is from line 2, etc. I need to read the entire table however, the table length will vary.
This is what I have so far. It can read the table and pull out the numbers I need, however, I don't know how to put the numbers into the proper format. I want to say something recursive would be most efficient but I'm unsure of how to do that. Thank you in advance.
#!/usr/bin/python
file = open("test_m.rdb")
while 1:
line = file.readline()
i = line.split()
flux = i[2]
observed = i[4]
if not line:
break
with open("test_m.rdb") as inf:
results = [(i[2],i[4]) for i in (line.split() for line in inf)]
result_string = ",".join(str(res) for res in results)
or a more general formatter:
result_string = ", ".join("('{2}', '{4}')".format(*res) for res in results)
Very simple solution:
with open('data') as data:
print ', '.join('(%s, %s)' % (x.split()[2], x.split()[4])
for x in data.readlines())
Just use readlines to iterate over the lines in the file.
assuming that two values you got are str1 and str2
//inside a loop which iterates through your values
strTable = strTable + ['('+str1+')','('+str2+')']
hope oit will work, if it dont, comment , i will solve it.

Categories

Resources