Note that there are some explanatory texts on larger screens.

plurals
  1. POpython--import data from file and autopopulate a dictionary
    text
    copied!<p>I am a python newbie and am trying to accomplish the following. A text file contains data in a slightly weird format and I was wondering whether there is an easy way to parse it and auto-fill an empty dictionary with the correct keys and values.</p> <p>The data looks something like this</p> <pre><code>01&gt; A B 2 ##01&gt; denotes the line number, that's all 02&gt; EWMWEM 03&gt; C D 3 04&gt; EWWMWWST 05&gt; Q R 4 06&gt; WESTMMMWW </code></pre> <p>So each pair of lines describe a full set of instructions for a robot arm. For lines 1-2 is for arm1, 3-4 is for arm 2, and so on. The first line states the location and the second line states the set of instructions (movement, changes in direction, turns, etc.)</p> <p>What I am looking for is a way to import this text file, parse it properly, and populate a dictionary that will generate automatic keys. Note the file only contains value. This is why I am having a hard time. How do I tell the program to generate armX (where X is the ID from 1 to n) and assign a tuple (or a pair) to it such that the dictionary reads.</p> <pre><code>dict = {'arm1': ('A''B'2, EWMWEM) ...} </code></pre> <p>I am sorry if the newbie-ish vocab is redundant or unclear. Please let me know and I will be happy to clarify.</p> <p>A commented code that is easy to understand will help me learn the concepts and motivation.</p> <p>Just to provide some context. The point of the program is to load all the instructions and then execute the methods on the arms. So if you think there is a more elegant way to do it without loading all the instructions, please suggest.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload