reading old BASIC data block

what is the trick to read old Data Block from BASIC program

I got some data like this

DATA 0.25 , .8 , 78 , 76 , .25 , .219 , .331
DATA 0.33 , .8 , 78 , 76 , .328 , .289 , .437
DATA 0.5 , .8 , 78 , 76 , .49 , .39 , .62
DATA 0.75, 1.1 , 78 , 76 , .73 , .59 , .94

so how can this be ported to a list in python
with min of commands?

thanks
happy bl

I can make a list with the data lines
and make a new list in python
but this require to do some manual edit of the lines

so wondering if there is faster way with minim of manual edit of the data lines?

thanks
happy bl

Each DATA line can be read from the file, cleaned with some string slicing (or regexes, if the things to clean up are not fixed-length), then split()-ed to get a list of strings that can be converted to numbers.

RickyBlender, using a capable text editor (e.g. NoteTab Light), you should be able to do an “Search and Replace” on the DATA prefix on each line, so that this…

DATA 0.25 , .8 , 78 , 76 , .25 , .219 , .331
DATA 0.33 , .8 , 78 , 76 , .328 , .289 , .437
DATA 0.5 , .8 , 78 , 76 , .49 , .39 , .62
DATA 0.75, 1.1 , 78 , 76 , .73 , .59 , .94

becomes this (being sure to disregard the “quote” in the top-left of these quoted text bubbles):

newArray.extend([0.25 , .8 , 78 , 76 , .25 , .219 , .331])
newArray.extend([0.33 , .8 , 78 , 76 , .328 , .289 , .437])
newArray.extend([0.5 , .8 , 78 , 76 , .49 , .39 , .62])
newArray.extend([0.75, 1.1 , 78 , 76 , .73 , .59 , .94])

…also adding a ]) at the end of each line.

To do this in NoteTab Light, paste your code into a new Python code text file (so as to keep your original BASIC code safe), and then bring up the Search / Replace window.

Click in the Find what box and type DATA and include one space after the last A in DATA.

Next, click in the Replace with box and type newArray.extend([

After that, click Replace All.

Once this is done, place the cursor at the top of the text file and bring up the Search / Replace window again.

In the Find what box, type ^P

– the ^ character being a shift 6 on a standard computer keyboard.

In the Replace with box type ])^P

After that, click Replace All.

At the very beginning of the new Python code text file, type this line:

newArray =

The new Python code text file should resemble something like this:

newArray =

newArray.extend([0.25 , .8 , 78 , 76 , .25 , .219 , .331])
newArray.extend([0.33 , .8 , 78 , 76 , .328 , .289 , .437])
newArray.extend([0.5 , .8 , 78 , 76 , .49 , .39 , .62])
newArray.extend([0.75, 1.1 , 78 , 76 , .73 , .59 , .94])

…and you can test-print your newly created Python list / array by adding these lines to the end of this new Python code text file:

print (newArray)

for n in newArray:print(n)

[Technical note: please be sure the print(n) line is indented once (one tab in) in the actual Python text file, as the forum spacing / indentation does not appear to carry over properly when copying / pasting this example.]

So, to summarize, this is what the search/replaced original code should look like with the additional printout code included at the end:

newArray =

newArray.extend([0.25 , .8 , 78 , 76 , .25 , .219 , .331])
newArray.extend([0.33 , .8 , 78 , 76 , .328 , .289 , .437])
newArray.extend([0.5 , .8 , 78 , 76 , .49 , .39 , .62])
newArray.extend([0.75, 1.1 , 78 , 76 , .73 , .59 , .94])

print (newArray)

for n in newArray:print(n)

I hope this possible method proves to be helpful to you =)

Best of luck with your project.

RobertT

it is one way to edit the lines
or read the data from a file

but if possible I was hoping for something that could modify the data lines inside python itself
with a minimum of manual edit

mind you I found a way to do it but still had to edit manual each data line a little
to make a long list

MOT12 = [ DATA , 0.25 , .8 , 78 , 76 , .25 , .219 , .331 ,
DATA , 0.33 , .8 , 78 , 76 , .328 , .289 , .437 ]

then rebuild a new list of list

thanks for feedback
happy bl

My proposed edit / search / replace methodis as minimal and easy as it gets, I think, since the text editor would literally be modifying all the DATA lines at once after you click Replace All.

So, while there are two steps in the process I described - search/replacing DATA and modifying the end of the lines so as to create an acceptable Python data list - this all could happen in less than a minute without you having to manually edit the lines.

The search / replace operation occurs near instantaneously.

And, while the method I described reference the NoteTab Light text editor, this very same process could be successfully carried out in any other comparably capable text editor.

RobertT

guess 2 step replace is not that much!

I have Notepad++

what is thisI
in the Find what box, type ^P
is it like end of line ?

thanks

Given you have txt file with lines as you describe:


import re
fname = 'DATAfile.txt'
with open(fname) as fil:
      mot = [re.sub('DATA , ','',line.rstrip('
')) for line in fil]

print(mot) would give list:
['0.25 , .8 , 78 , 76 , .25 , .219 , .331 ', ‘0.33 , .8 , 78 , 76 , .328 , .289 , .437’]

Yes, it’s NoteTab’s characters for non-REGEX (PERL Regular EXpression) for end of line detection.

In Notepad++ you can use the Extended search mode in the Search / Replace box to modify the ends of the DATA lines. Check Extended, and then:

in the Find what box enter \r

and in the Replace with box enter ])\r

That should do it.

Some text files end have both a carriage return and a line feed (CR+LF), and in those cases it would probably be necessary to enter in the Find what box these characters:

and then have this in the Replace with box:

])

I think you get the idea now =)

Good luck!

RobertT

thanks for clarifications

how would you compare the 2 editor ?

is it equivalent ?

happy bl

I definitely prefer NoteTab Light, although Notepad++ is great as well and likely has the edge when it comes to language-specific syntax highlighting.

NoteTab Light has a number of handy text / line reformatting functions under its Modify menu.

Among other features, Notepad++ has a nice macro recording feature which can be particularly useful to semi-automate repetitive key sequences / text edits.

Both support regular expression find / search / replace, which can be incredibly important when performing complex edits on text and source code files.

It’s probably best to try them both to see what works best for you.

RobertT