Python: open dat-files, read data

In summary, The person is having trouble with two programs and has provided the code for both. In the first program, they are not getting any output from the function "andre(f)" and in the second program, they are trying to use numbers but are having trouble with formatting. They also mentioned a file called "densities.dat" and provided its contents, but are unable to upload it. The expert suggests reading the lines of the file before trying to use them in the second program. They also mention that the person should have included their solution, but they no longer know what it was.
  • #1
MaxManus
277
1

Homework Statement


I have two programs and neither works. In the first program I don't get any autput from andre(f):

f = open("densities.dat", "r")

def andre(f):
densities = {}
for line in f:
density = float(line[12:])
name = line[0:10].strip()
densities[name] = density

return densities

and in the second program I want to be able to use the numbers and get therfor strip "," and make a float

from math import *
f = open("output.txt", "r")
print type(1e-4)
epsilon = []
exact_error = []
n = []
for line in f:
words = line.split(",")
words = line.split()
print float(words[1].strip(","))
 

Attachments

  • output.txt
    225 bytes · Views: 750
Technology news on Phys.org
  • #2
I didn't manage to upload densities.dat
densities.dat
air 0.0012
gasoline 0.67
ice 0.9
pure water 1.0
seawater 1.025
human body 1.03
limestone 2.6
granite 2.7
iron 7.8
silver 10.5
mercury 13.6
gold 18.9
platinium 21.4
Earth mean 5.52
Earth core 13
Moon 3.3
Sun mean 1.4
Sun core 160
proton 2.8E+14
 
  • #3
I got the answear and I will post it later today.
 
  • #4
so what was the solution after all ?
 
  • #5
I'm sorry, but I don't longer know and I'm no longer able to find the solution.
Again I should have posted my solution.
 
  • #6
You opened the file, but you didn't read the file. Normally the sequence goes something like this:

Code:
file = open("filename","r")
lines = file.readlines() # This is what you forgot
file.close()
for line in lines:
   name = float(line.strip().split()[0]) # Or something like this...
   ...
 

Related to Python: open dat-files, read data

What is Python?

Python is a programming language that is used for a variety of purposes, including data analysis and scientific computing.

How do I open dat-files in Python?

To open a dat-file in Python, you can use the built-in function open(). This function takes two arguments: the name of the file and the mode in which you want to open it (e.g. "r" for reading or "w" for writing).

How do I read data from a dat-file in Python?

Once you have opened a dat-file in Python, you can use the read() method to read the data from the file. This method will return the contents of the file as a string.

How can I manipulate the data from a dat-file in Python?

Python has a wide range of built-in functions and libraries for manipulating data. You can use these to perform operations such as sorting, filtering, and transforming the data from a dat-file.

Can I use Python to analyze large datasets in a dat-file?

Yes, Python is a powerful tool for analyzing large datasets. It has libraries like NumPy and Pandas that are specifically designed for efficient data analysis and manipulation. Additionally, Python has the ability to work with data in parallel, making it suitable for handling large datasets.

Similar threads

  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
8
Views
912
  • Programming and Computer Science
Replies
3
Views
361
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
8
Views
401
Replies
3
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top