tags:

views:

434

answers:

6

Dear Sir/Madam.

My name is Duc from University of Technology. I have faced a difficult problem. That is reading data file:

///
* ABC Names
A-06,B-18,
* Data
 1.727e-01, 1.258e-01, 2.724e-01, 2.599e-01,-3.266e-01,-9.425e-02,-6.213e-02, 1.479e-01,
 1.219e-01, 1.174e-01, 2.213e-01, 2.875e-01,-2.306e-01,-3.900e-03,-5.269e-02, 7.420e-02,
 2.592e-01, 2.513e-01, 2.242e-01, 2.620e-01,-1.346e-01,-6.844e-02,-4.139e-02, 9.502e-02,
 1.981e-01, 1.937e-01, 2.336e-01, 1.617e-01,-4.240e-02, 2.285e-02, 1.878e-02, 1.064e-01,
 9.562e-02, 6.727e-02, 1.135e-01, 6.765e-02,-7.850e-02, 6.711e-02, 1.317e-02, 8.367e-02, 
* Starting position
     -.5000E+01
///

Code run in Python? I try using readline(), readlines() functions but no result.

if you have experiences about this please tell me. Thanks for your kind help. Best regards,

A: 

Suppose the file's named "abc.txt" and is in the current directory; then the following Python script:

f = open("abc.txt")
all_lines = f.readlines()

will read all the lines into list of strings all_lines, each with its ending \n and all.

What you want to do after that we can't guess unless you tell us, but the part you're asking about, the reading, should be satisfied.

Alex Martelli
A: 

Assuming you want to get the block from *Data to *Starting Position,

f=0
for line in open("file"):
    line=line.strip()
    if "Starting" in line:
        f=0
    if "Data" in line:
        f=1
        continue
    if f:
        print line

the idea is to set a flag. if *Data is hit, set flag. the print all lines if flag is set. If *Starting is hit, turn off the flag.

ghostdog74
A: 

This ought to work for files with block names 'a', 'b', and 'c'. It will create a dictionary with keys as block titles like so:

{'a':['line1','line2'],'b':['line1'],'c':['line1','line2','line3']}

code:

block_names = ['b','a','c']

for line in open('file.txt'):
    block_dict = {}  #dict to populate with lists of lines
    block = []  # dummy block in case there is data or lines before first block
    ck_nm = [blk_nm for blk_nm in block_names if line.startswith(blk_nm)]  #search block names for a match
    if ck_nm: # did we find a match?
        block_dict[ck_nm[0]] = block = []  # set current block
    else:
        block.append(line)  #..or line.split(',') ..however you want to parse the data
bpowah
A: 

Without any other information...

data = [
1.727e-01, 1.258e-01, 2.724e-01, 2.599e-01,-3.266e-01,-9.425e-02,-6.213e-02, 1.479e-01,
1.219e-01, 1.174e-01, 2.213e-01, 2.875e-01,-2.306e-01,-3.900e-03,-5.269e-02, 7.420e-02,
2.592e-01, 2.513e-01, 2.242e-01, 2.620e-01,-1.346e-01,-6.844e-02,-4.139e-02, 9.502e-02,
1.981e-01, 1.937e-01, 2.336e-01, 1.617e-01,-4.240e-02, 2.285e-02, 1.878e-02, 1.064e-01,
9.562e-02, 6.727e-02, 1.135e-01, 6.765e-02,-7.850e-02, 6.711e-02, 1.317e-02, 8.367e-02,
]
endolith
+2  A: 

Here's a complete guess at some code that might load the type of file this is an example of, but which should be a little robust:

f = open("mdata.txt")

data_dict = {}
section = None
data_for_section = ""
for line in f:
    line = line.strip() #remove whitespace at start and end

    if section != None and (line[0] == "*" or line == "///"):
        # if we've just finished a section, put whatever we got into the data dict
        data_dict[section] = [bit for bit in data_for_section.split(",") if bit != ""]

    if line[0] == "*":
        # "*" denotes the start of a new section, probably, so remember the name
        section = line [2:]
        data_for_section = ""
        continue
    data_for_section += line

f.close()
#got the data, now for some output
print "loaded file. Found headings: %s"%(", ".join(data_dict.keys()))

for key in data_dict.keys():
    if len(data_dict[key])>5:
        print key, ": array of %i entries"%len(data_dict[key])
    else:
        print key, ": ", data_dict[key]

which outputs for your file:

loaded file. Found headings: ABC Names, Data, Starting position
ABC Names :  ['A-06', 'B-18']
Data : array of 40 entries
Starting position :  ['-.5000E+01']

of course, you'd probably want to convert the list of data strings to floating point numbers in the case of data and starting position:

startingPosition = float(data_dict["Starting position"][0])
data_list_of_floats = map(float, data_dict["Data"])

But as to the ABC Names and how they combine with the rest of the file, we'd need some more information for that.

Markus
A: 

Thank you for your reply. However, I attempted to start with Markus's code but failed when between this line and that line exist empty. Example: /// "filename.txt" have: * ABC Names A-06,B-18, * Data 1.727e-01, 1.258e-01, 2.724e-01, 2.599e-01,-3.266e-01,-9.425e-02,-6.213e-02, 1.479e-01, 1.219e-01, 1.174e-01, 2.213e-01, 2.875e-01,-2.306e-01,-3.900e-03,-5.269e-02, 7.420e-02, 2.592e-01, 2.513e-01, 2.242e-01, 2.620e-01,-1.346e-01,-6.844e-02,-4.139e-02, 9.502e-02, 1.981e-01, 1.937e-01, 2.336e-01, 1.617e-01,-4.240e-02, 2.285e-02, 1.878e-02, 1.064e-01, 9.562e-02, 6.727e-02, 1.135e-01, 6.765e-02,-7.850e-02, 6.711e-02, 1.317e-02, 8.367e-02, * Starting position -.5000E+01

  • Total number of clicks (modified) 18

  • Clicker times (modified) 448 748 /// Error when between "Starting position" and "Total number of clicks (modified)" have empty line. I need data: 1.727e-01, 1.258e-01, 2.724e-01, 2.599e-01,-3.266e-01,-9.425e-02,-6.213e-02, 1.479e-01, 1.219e-01, 1.174e-01, 2.213e-01, 2.875e-01,-2.306e-01,-3.900e-03,-5.269e-02, 7.420e-02, 2.592e-01, 2.513e-01, 2.242e-01, 2.620e-01,-1.346e-01,-6.844e-02,-4.139e-02, 9.502e-02, 1.981e-01, 1.937e-01, 2.336e-01, 1.617e-01,-4.240e-02, 2.285e-02, 1.878e-02, 1.064e-01, 9.562e-02, 6.727e-02, 1.135e-01, 6.765e-02,-7.850e-02, 6.711e-02, 1.317e-02, 8.367e-02, in block "* Data" and sometime need data
    448 748 in block "* Clicker times (modified)"

Your example is too big and is illegible. Presuming you want to ignore blank and empty lines: after Markus's line that reads `line = line.strip()` add another line `if not line: continue`
John Machin