Python - Data Manipulation
Python - Data Manipulation
(OP)
Hello everyone,
I am getting started with python programming and I am facing some problems regarding data manipulation.
Please consider the following data:
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.
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.
RE: Python - Data Manipulation
[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
RE: Python - Data Manipulation
Could you tell me how to read all data and do that for all lines?
Thanks
RE: Python - Data Manipulation
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
RE: Python - Data Manipulation
After that, I will try to post here the solution for future users :)
RE: Python - Data Manipulation
CODE --> python
Now, I just need to give the instruction to save the contents in the file2.txt
Thank you for your help!
RE: Python - Data Manipulation
you almost never want to create a range to iterate over as an index. the pythonic way to achive this would be:-
CODE
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
RE: Python - Data Manipulation
I think here is the complete solution:
CODE -->
Suggestions/comments will be very welcome! Thank you for your help
RE: Python - Data Manipulation
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
RE: Python - Data Manipulation
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 -->
and I want to eliminate the duplicated entries in column 2? in order to have this:
CODE -->
My problem is that I get always an extra line like this:
CODE -->
The code that I am using is:
CODE -->
RE: Python - Data Manipulation
CODE -->
Using line.strip. What do you think?
RE: Python - Data Manipulation
this works for two numbers separated by a space.
CODE -->
Any comments?