Python: open dat-files, read data

  • Context: Python 
  • Thread starter Thread starter MaxManus
  • Start date Start date
  • Tags Tags
    Data Python
Click For Summary

Discussion Overview

The discussion revolves around issues related to reading data from files in Python, specifically focusing on two programs that attempt to process data from a file named "densities.dat" and another file "output.txt". Participants explore problems with file reading and data extraction.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant shares a code snippet that attempts to read densities from a file but reports no output from the function andre(f).
  • The participant provides a sample of the contents of "densities.dat", which includes various materials and their corresponding densities.
  • Another participant points out that the original code does not read the file correctly, suggesting that the participant forgot to use file.readlines() to read the lines from the file.
  • There are indications of confusion regarding the solution to the problem, with one participant stating they will post the answer later, while another expresses uncertainty about the solution and regrets not posting it earlier.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the solution to the problem, and there is uncertainty regarding the correct approach to reading and processing the data from the files.

Contextual Notes

There are limitations in the provided code snippets, such as missing steps for reading the file contents and potential issues with data formatting that are not fully resolved in the discussion.

MaxManus
Messages
268
Reaction score
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

Technology news on Phys.org
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
 
I got the answear and I will post it later today.
 
so what was the solution after all ?
 
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.
 
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...
   ...
 

Similar threads

  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
3
Views
4K