remove some string from a text file - python-3.7

I have a text file and I want to remove a part of text using Python 3.7.
Example:
helloooooooooooooo ### 122
How Are uuuuuuuuuuuuuuuuuuuuu?
GigaEthernet 0 1 0
GigaEthernet 1 2 0
GigaEthernet 2 1 3
helloooooooooooooo ### 122
helloooooooooooooo ### 122
i want the text file change to:
GigaEthernet 0 1 0
GigaEthernet 1 2 0
GigaEthernet 2 1 3
It maybe "FastEthernet" instead of GigaEthernet so we should consider "Ethernet" as a key word).

Instead of giving you a complete solution, I'll help on how to approach this kind of problem.
What do you have? A file.
What do you want? A modified file.
First, you need to load in the file as a data structure that you can work with, see: How to read a text file into a list or an array with Python
Second, you need to remove some lines, see: How to remove specific element in an array using python
Third, you need to save the changes to a file, see: Correct way to write line to file?
I hope that this helps.

Related

Powershell Output Question when running Forms

When I run my Forms code I have different objects that are added to the Form (buttons, labels, etc) I attache the objects to the form by using the command $Form1.Controls.Add([ObjectType]).
My question is, when I run my code I get an instant sequence of numbers in my console and output dialogue box of:
0 1 2 3 4 5 6 7 8 9 0 1 2
When after I click Submit button the string "OK" is added to the numbers shown above
0 1 2 3 4 5 6 7 8 9 0 1 2 OK
Why is this happening and how can I remove these or atleast omit them from displaying.
The OK displays once the Submit button is pressed.
OK
Some actions like .Add() are producing output. To prevent this, pipe the output into the [void] by adding | Out-Null at the end of the line or [void] directly infront of the variable that is used, like:
$foo.SomethingThatGeneratesOutput() | Out-Null
or
[void]$foo = SomethingThatGeneratesOutput
As T-Me has stated, to prevent output being generated when executing methods as you are use [Void].
[Void]$Form1.Controls.Add([ObjectType])
If your code is still returning unwanted data, open the script in PowerShell ISE, and execute the script line by line (select the line and press F8). This will help you determine which line of code is generating output still.

readtable and invalid variable names

I have a script that converts a simulation data file that has variable names like force(1), velocity(2), etc. I have a script that converts the data file to CSV file and up to Matlab 2016b, I was able to import that CSV file to Workspace as a table by using 'readtable'. Readtable would issue a warning that says Matlab modifies those invalid variable names, but still import successfully by replacing the parentheses with underscores: a very minimum amount of modification.
Now with 2017b, readtable doesn't do what it used to do and the imported table variables have completely arbitrary names such as var1, var2, and such: a significant amount of modification.
How can I force readtable modify the variable names like it used to do? Here's a snap shot of the CSV file and it's the "S_Out_DE10(1)" variable name causing the havoc.
I can't use the GUI because I actually bring in multiple files and create table variables in a batch.
I open the CSV file, Cntr+S or Save button, say 'OK' when asked to keep the same format, and 'No' to the same question when closing the file; all without touching a single cell.
Here is a text format of some of the data:
TIME S_Out_DE10(1) Vph x_ehsv mA_ehsv
0 0 0 0 0
0.001 0 0 0 0
0.002 0 0 0 0
0.003 0 0 0 0
0.004 0 0 0 0
Now, the funny thing is if you copy/past the above and "save" it into a CSV file, that "saving" again makes it work.
Instead of using a CSV file with headers (for which it is not designed), you can use xlsread to directly read your Excel files instead.

copying every nth line and duplicating it on it's following line

I am trying to make test files for the project, and I figured in order to make a bradycardia test file from an example file of a normal ECG.
Therefore I would need to copy every third line and insert it into the next line.
for example:
a = [
1
2
3
4
5
6
7
8
9
10]
and I want:
b = [
1
2
3
3
4
5
6
6
7
8
9
9
10]
and so on... but since the file is 6000 characters long, obviously i cannot manually copy it. And I need it to be 9000 characters long I've tried looking online on how to do this, and am having no luck.
Any suggestions?
b=zeros(floor(4/3*length(a)),1);
b(1:4:end)=a(1:3:end);
b(2:4:end)=a(2:3:end);
b(3:4:end)=a(3:3:end);
b(4:4:end)=a(3:3:end);
Another way:
b = a(sort([1:numel(a) 3:3:numel(a)]))
And here is a third faster and simpler method
b = a(round(1:0.75:numel(a)))
This only works if length(a) is a multiple of 3, but seems to be faster than the other answers, at least for large vectors:
b = reshape([reshape(a,3,[]); a(3:3:end).'],[],1);

How to skip a column using txt2mat

I´m trying to import some csv files in matlab, but csvread is too slow.
I´m using txt2mat, but i don´t know how to skip the first column in the import.
This is the way im trying
myimportedfile = txt2mat(myfile,'ReadMode','block',1) %im skipping the headers too.
The reason i need to skip is beacause the first column is non-numerical data.
Is there a way to do this with txt2mat or is there a better way?
Tks in advance.
textscan gives you the ability to skip columns. It reads in data using an fprintf-like format string.
Example file:
Val1 Val2 Val3
1 2 3
4 5 6
7 8 9
Code:
tmp = textscan('example.txt', '%i %*i %i') % the * indicates fields to ignore
tmp{:}

How to extract certain columns from a big Notepad text file?

I have a big text file and the data in it are in 5 columns, but I need just the first and the last column of that.
It will take many days and probably with mistake if I want to enter the data of this two column one-by-one from here to another file.
Is there a fast way to do this?
For example:
1 1.0000000000000000 0.0000000000 S {0}
2 1.5000000000000000 0.3010299957 C {2}
3 1.7500000000000000 0.6020599913 S {0,2}
4 2.0000000000000000 0.7781512504 C {3}
5 2.3333333333333333 1.0791812460 C {3,2}
6 2.5000000000000000 1.3802112417 S {3,0,2}
7 2.5277777777777778 1.5563025008 S {0,3}
8 2.5833333333333333 1.6812412374 S {3,0,0,2}
9 2.8000000000000000 1.7781512504 C {5,2}
10 3.0000000000000000 2.0791812460 C {5,0,2}
I need the first column (numbering) and the last inside { }.
ALT + Left Mouse Click puts you in Column Mode Select. It's quite an useful shortcut that may help you.
in Notepad++, you can use regular expression to do replacement:
the regex for find and replace is:
^( +\d+).+\{([\d,]+)\}$
\1 \2
then can change the:
1 1.0000000000000000 0.0000000000 S {0}
2 1.5000000000000000 0.3010299957 C {2}
3 1.7500000000000000 0.6020599913 S {0,2}
4 2.0000000000000000 0.7781512504 C {3}
5 2.3333333333333333 1.0791812460 C {3,2}
6 2.5000000000000000 1.3802112417 S {3,0,2}
7 2.5277777777777778 1.5563025008 S {0,3}
8 2.5833333333333333 1.6812412374 S {3,0,0,2}
9 2.8000000000000000 1.7781512504 C {5,2}
10 3.0000000000000000 2.0791812460 C {5,0,2}
to:
1 0
2 2
3 0,2
4 3
5 3,2
6 3,0,2
7 0,3
8 3,0,0,2
9 5,2
10 5,0,2
if not want the leading space, then use:
^( +\d+).+\{([\d,]+)\}$
\1 \2
will change to:
1 0
2 2
3 0,2
4 3
5 3,2
6 3,0,2
7 0,3
8 3,0,0,2
9 5,2
10 5,0,2
You should use awk or gawk which is available on windows platform also. Use gawk "{print $1,$5}" inpfile > outfile. I copied your file named it 'one'. You can see the output which consists of 1st and 5th column of your file.
>gawk "{print $1, $5}" one
1 {0}
2 {2}
3 {0,2}
4 {3}
5 {3,2}
6 {3,0,2}
7 {0,3}
8 {3,0,0,2}
9 {5,2}
10 {5,0,2}
You can import it into Excel and manipulate it there.
If you are using .NET, FileHelpers may save you a lot of time. From your post we can't tell what technology you are hoping to use to accomplish this.
Ultraedit has a tool for selecting columns and opens large files (I tried a 900 Mb file on a 2008 desktop and it opened in 3 minutes). I think it has a demo version fully operational.
Excel could work if you do not have too many rows.
Cheers,
One more way is to copy the data to MS word file.
Then use
{Alt + left mouse click}
Then you can drag on the selected column and you can see only a single column is selected.
Copy and paste wherever you want.
There is only one way to convolve ungodly amounts of data. That is with the command prompt.
$cat text.txt | sed 's/{.*,//;s/ */ /g;s/[{}]//g' | awk '{print $1","$5}' > clean_text.csv
This 15 second fix is not available in Windows OS. It will take you less time to download and install Linux on that old dead computer in your closet than it will to get your data in and out of Excel.
Happy coding!