Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Python - Data Manipulation

Status
Not open for further replies.

nvhuser

Programmer
Apr 10, 2015
48
DE
Hello everyone,

I am getting started with python programming and I am facing some problems regarding data manipulation.
Please consider the following data:

Code:
2.9        -4.0   50.0
3.4        -5.0  150.0
2.9        -1.0  350.0
2.4        -4.0   50.0
2.0        -2.0  110.0

The numbers are available in an 8-digit system, it means that the first number should be between the column 1 to 8, while the second one should be between the column 9 and 16, and so on.

I would like to create a script that reads this information stored in a file "file.txt" available in a given directory and evaluate the sum of the three components, saving the file "file2.txt". Thank you for your help.
 
you could try slicing but with the example data split would suffice

Code:
>>>a= "2.9        -4.0   50.0"
>>>b=a.split()
>>>b
['2.0','-4.0','50.0'
>>>float(b[0]+float(b[1])+float(b[2])
48.9
[code]

A Maintenance contract is essential, not a Luxury.
Do things on the cheap & it will cost you dear
 
Thank you IPGuru, your commands work fine!
Could you tell me how to read all data and do that for all lines?
Thanks
 
i suggest you check the online tutorial for open.

in principle you open a file for reading

loop over each line in turn (hint a for loop is ideal here)
process the data

to give you a full solution would not help your learning.

post some code & we well you what you are doing wrong (ant what you are doing right) and po9sibly sugest some imrovements.
if the code fails copy the python traceback as well

A Maintenance contract is essential, not a Luxury.
Do things on the cheap & it will cost you dear
 
Thank you IPGuru, I will check the tutorial.
After that, I will try to post here the solution for future users :)
 
As promised, here is the python code:

Python:
#!/bin/python

file_object=open('file1.txt','r')
f=file_object.readlines()

count=len(f)
for i in range(0,len(f)):
        b=f[i].split()
        c=float(b[0])+float(b[1])+float(b[2])
        print c

Now, I just need to give the instruction to save the contents in the file2.txt
Thank you for your help!
 
your method for iterating over the input, while valid is not very pythonic

you almost never want to create a range to iterate over as an index. the pythonic way to achive this would be:-
Code:
#!/bin/python

file_object=open('file1.txt','r')
for line in file_object:[URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?qid=1750241[/URL]
        b=line.split()
        c=float(b[0])+float(b[1])+float(b[2])
        print c

if you open a 2nd file for writing & replace your print statement with a write to file you project will be complete.
the official python tutorials should give you plenty of examples
(google python write to file & also python with)

A Maintenance contract is essential, not a Luxury.
Do things on the cheap & it will cost you dear
 
Hi IPGuru, thank you for your Tip!
I think here is the complete solution:

Code:
#!/bin/python
file_object=open('file1.txt','r')
file_object2=open('file2.txt','w')
for line in file_object:
        b=line.split()
        c=float(b[0])+float(b[1])+float(b[2])
        file_object2.write(str(c)+"\n")

Suggestions/comments will be very welcome! Thank you for your help
 
looks fine to me
For robustness in a real world program you should have some error checking.
(what happens if you cant open the input file?, what happens if you cant open the output file, what happens if your routine fills all of the available disk space etc.)

These are things you will learn about as you progress through the tutorials


A Maintenance contract is essential, not a Luxury.
Do things on the cheap & it will cost you dear
 
Hi IPGuru, thank you for your advices!
In fact, I can add a if loop to check if it the file can open

I have another question:

Imagine that I have a matrix with four rows and two columns, like the following:

Code:
2 3
4 4
5 4
6 2

and I want to eliminate the duplicated entries in column 2? in order to have this:

Code:
2 3
4 4
6 2

My problem is that I get always an extra line like this:

Code:
2 3

4 4

6 2

The code that I am using is:
Code:
#/bin/python

file_object=open('file1.txt','r')
file_object2=open('file2.txt','w')

read_data=file_object.readlines()
nd=[]

for line in read_data:
        s=line
        if s[2] not in nd:
                nd.append(s[2])
                file_object2.write(str(line)+"\n")
Thanks in advance.
 
I think one solution would be:

Code:
#/bin/python

file_object=open('file1.txt','r')
file_object2=open('file2.txt','w')

read_data=file_object.readlines()
nd=[]

for line in read_data:
        s=line
        if s[2] not in nd:
                nd.append(s[2])
                line = line.strip('\n')
                file_object2.write(str(line)+"\n")

Using line.strip. What do you think?
 
The previous code only works for 1-digit numbers
this works for two numbers separated by a space.

Code:
#/bin/python
file_object=open('sample.txt','r')
file_object2=open('sample2.txt','w')

read_data=file_object.readlines()
nd=[]

for line in read_data:
        s=line
        b=s.split()
        if b[1] not in nd:
                nd.append(b[1])
                line = line.strip('\n')
                file_object2.write(str(line)+"\n")

Any comments?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top