1

I have the following data, currently stored in a Python list:

formant_data = [ 
    'ɑ',  
     125, 150, 10,
     580,  50, 10,
     780,  50,  9,
    1010,  50, 10,
    2530, 100,  9,
    4400, 150,  8,
    5300, 200,  8,
    5880, 200,  7,
    7420, 200,  7,
    8860, 200,  7,

    'a',
     168, 120, 10,
     680,  50,  9,
    1000,  50,  9,
    1500, 150,  8,
    2500, 200,  7,
    5200, 800,  7,
    7200, 400,  6,
    8500, 400,  6,

    'ɛ',
     150, 120, 10,
     700,  50,  8,
     750,  50,  8,
    1800, 150,  8,
]

And I am unpacking it as follows:

import numpy as np

vowels = deque()
acc = deque()
for x in reversed(formant_data):
    if isinstance(x,str):
        symbol = x
        print(symbol)
        data = np.reshape( acc, (-1,3) )
        vowels.appendleft(data)
        acc.clear()
    else:
        acc.appendleft(x)
return vowels

I wish to move the data into a text file, and read it in.

What's the simplest way to do it?

I don't mind fiddling around with the data file format. The main objective is that it be easily readable.

EDIT: Maybe I can just save it as data.py and import data.py ? problem: I want it to reside in input/data.py

EDIT: %run magic?

P i
  • 29,020
  • 36
  • 159
  • 267

3 Answers3

1

OPTION 1: (Similar option to your EDIT)

You can store simply your list in a file called data_file.py and then do from data_file import *

OPTION 2: (given by jez in the comments)

More efficient, but a little bit tricky to understand. You can store simply your list in a file called data_file.py and then do in your code:

container = {}
exec(open('data_file.py').read(), container)

OPTION 3:

You can store all the content in the list in a .txt or .whatever file, and then read all the content of the file and split it by "," as follows:

with open('input.txt', 'r') as input:
    content = input.read()
    list = content.split(',')

And then you'll be able to do whatever you want with your list.

Community
  • 1
  • 1
arodriguezdonaire
  • 5,396
  • 1
  • 26
  • 50
  • 1
    Instead of `import`, use `execfile`. If you want some control over where the variables end up, say `container = {}; execfile(filename, container)`. The reason for preferring `execfile` over `import` is that, once you have imported `data.py` once, its content will be cached and subsequent imports of the same file will yield the old data regardless of recent changes. With either `import` or `execfile`, note the security implications, since you're allowing your interpreter to execute arbitrary commands written in what may or may not be a benign "data" file. – jez Nov 12 '15 at 16:10
  • 1
    You're right, but `execfile` is not valid for all python versions – arodriguezdonaire Nov 12 '15 at 16:14
  • 1
    Argh http://stackoverflow.com/questions/436198/what-is-an-alternative-to-execfile-in-python-3-0 why Python WHY? – P i Nov 12 '15 at 16:14
1

For better security than import or exec, while maintaining readability, use the standard json module:

To save:

import json
with open( filename, 'wt' ) as datafile:
    json.dump( formant_data, datafile, indent=3 )

To load:

import json
with open( filename, 'rt' ) as datafile:
    formant_data = json.load( datafile )

This gives you flexibility as to how you want to format the data. json serialization supports the dict type, which is presumably a more natural way of associating the numbers with each of your phonemes.

jez
  • 14,867
  • 5
  • 37
  • 64
  • Removed the alternative `pickle`-based code since `pickle` isn't secure either. – jez Nov 12 '15 at 16:33
0

Apologies for adding iPython to the question late -- I didn't think it was relevant, but it turns out iPython has a %run magic:

%run input/formant_data.py

While I'm going with this, I'm grateful for the other answers. I think import would require packaging, as the file is not in the current folder.

P i
  • 29,020
  • 36
  • 159
  • 267