extracting data from a text file (python) - python

i have two columns of numbers in a text file which is the columns of time and stress respectively which i get it from an analysis in abaqus finite element package ! i want to extract the time column and stress column in seperate lists ( a list for time and another list for stress ) . and then use this lists to do some other mathematical operations and . . .
my problem is how to create this lists ! my text file is as follows : (the first line of the text file and the four lines from the bottom of that is empty!)
X FORCE-1
0. 0.
10.E-03 98.3479E+03
12.5E-03 122.947E+03
15.E-03 147.416E+03
18.75E-03 183.805E+03
22.5E-03 215.356E+03
26.25E-03 217.503E+03
30.E-03 218.764E+03
33.75E-03 219.724E+03
37.5E-03 220.503E+03
43.125E-03 221.938E+03
51.5625E-03 228.526E+03
61.5625E-03 233.812E+03

You can read your file line by line
time = []
stress = []
count =0
with open("textfile.txt") as file:
for line in file:
line = line.strip() #removing extra spaces
temp = line.split(" ")
if count>=3 and temp[0].strip() : #checking empty string as well
time.append(temp[0].strip()) #removing extra spaces and append
stress.append(temp[len(temp)-1].strip()) #removing extra spaces and append
count+=1
print time
Output running above script
['0.', '10.E-03', '12.5E-03', '15.E-03', '18.75E-03', '22.5E-03', '26.25E-03', '30.E-03', '33.75E-03', '37.5E-03', '43.125E-03', '51.5625E-03', '61.5625E-03']

Related

Delete paragraph containing string in python

I have a file that contains blocks of information beginning and ending with the same phrase:
# Info block
Info line 1
Info line 2
Internal problem
ENDOFPARAMETERPOINT
I am trying to write a python code that deletes the entire block beginning with # Info block and ending with ENDOFPARAMETERPOINT once it detects the phrase Internal problem.
finds = '# Info block\nInfo line 1\nInfo line 2\nInternal problem\nENDOFPARAMETERPOINT'
with open(filename,"r+") as fp:
pattern = re.compile(r'[,\s]+' + re.escape(finds) + r'[\s]+')
textdata = fp.read()
line = re.sub(pattern,'',textdata)
fp.seek(0)
fp.write(line)
This code only works for one line but not the entire paragraph. Any suggestions are appreciated.
EDIT:
The code that works now is:
with open(filename,"r+") as fp:
pattern = re.compile(re.escape(finds))
textdata = fp.read()
line = re.sub(pattern,'',textdata)
fp.seek(0)
fp.write(line)
fp.truncate()
Why can't you just use pattern = re.compile(re.escape(finds))?
You can use two lists start_indexes and stop_indexes which contain respectively the start index to remove from and the end index to remove to. Then you can merge the two lists with the 'zip' method to have a matrix where each row has the start index and the end index of the rows to be removed. For each of these rows in the matrix you can create a list with the lines corresponding to the range of values and then remove the values contained in this list from the original list.
In this example the text to be processed divided into lines is stored in vals.
vals = ['string', '#blabla', 'ciao', 'miao', 'bau', 'ENDOFPARAMETERPOINT', 'as']
start_indexes = []
stop_indexes = []
for index, line in enumerate(vals):
if line[0] == '#':
start_indexes.append(index)
elif line == 'ENDOFPARAMETERPOINT':
stop_indexes.append(index)
for start, stop in zip(start_indexes, stop_indexes):
values_to_remove = [vals[x] for x in range(start, stop+1)]
for v in values_to_remove:
vals.remove(v)

Charting values using python

I have a log file which shows data sent in the below format -
2019-10-17T00:00:02|Connection(10.0.0.89 :0 ) r=0 s=1024
d=0 t=0 q=0 # connected
2019-10-17T00:00:02|McSend (229.0.0.70 :20001) b=1635807
f=2104 d=0 t=0
There will be multiple lines per file
How can I graph the b=value against the time (near the beginning on the line) but only from the McSend lines
Thanks
If you're not familiar with regular expressions - python regex documentation is a good place to start.
The simplest regex you probably need is r"^(\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d)\|.*McSend.*+b=(\d+)"
First group will allow you compare the timestamp and the second will give the value.
import re
pattern = r"^(\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d)\|.+McSend.+b=(\d+)"
#result is a list of tuples containing the time stamp and the value for b
result = re.findall(pattern, some_input)
You should read your file line by lines. Then scan for each line if it contains 'McSend'. If it does then retrieve the desired data.
You could do something like this :
b_values = []
dates = []
## Lets open the file and read it line by line
with open(filepath) as f:
for line in f:
## If the line contains McSend
if 'McSend' in line :
## We split the line by spaces ( split() with no arguments does so )
splited_line = line.split()
## First string chunk contains the header where the date is located
header = splited_line[0]
## Then retrieve the b value
for val in splited_line :
if val.startswith('b=') :
b_value = val.split("=",1)[1]
## Now you can add the value to arrays and then plot what you neet
b_values.append(b_value)
dates.append(header.split("|",1)[0]
## Do your plot

Finding the Characters in a Specific Line in a File

I am trying to find the characters in one specific line of my code. Say the line is 4.
My file consists of:
1. randomname
2. randomname
3.
4. 34
5. 12202018
My code consists of:
with open('/Users/eviemcmahan/PycharmProjects/eCYBERMISSION/eviemcmahan', 'r') as my_file:
data = my_file.readline(4)
characters = 0
for data in my_file:
words = data.split(" ")
for i in words:
characters += len(i)
print(characters)
I am not getting an error, I am just getting the number "34"
I would appreciate any help on how to get a correct amount of characters for line 4.
my_file.readline(4) does not read the 4th line, instead reads the next line but only the 4 firsts characters. To read a specific line you need to, for example, read all the lines and put them in a list. Then is easy to get the line you want. You could also read line by line and stop whenever you find yourself in the line you desired.
Going with the first approach and using the count method of strings, it is straight-forward to count any character at a specific line. For example:
line_number = 3 # Starts with 0
with open('test.txt', 'r') as my_file:
lines = my_file.readlines() # List containing all the lines as elements of the list
print(lines[line_number ].count('0')) # 0
print(lines[line_number ].count('4')) # 2

Python - How to split a list into two separate lists dynamically

I am using Python-3 and I am reading a text file which can have multiple paragraphs separated by '\n'. I want to split all those paragraphs into a separate list. There can be n number of paragraphs in the input file.
So this split and output list creation should happen dynamically thereby allowing me to view a particular paragraph by just entering the paragraph number as list[2] or list[3], etc....
So far I have tried the below process :
input = open("input.txt", "r") #Reading the input file
lines = input.readlines() #Creating a List with separate sentences
str = '' #Declaring a empty string
for i in range(len(lines)):
if len(lines[i]) > 2: #If the length of a line is < 2, It means it can be a new paragraph
str += lines[i]
This method will not store paragraphs into a new list (as I am not sure how to do it). It will just remove the line with '\n' and stores all the input lines into str variable. When I tried to display the contents of str, it is showing the output as words. But I need them as sentences.
And my code should store all the sentences until first occurence of '\n' into a separate list and so on.
Any ideas on this ?
UPDATE
I found a way to print all the lines that are present until '\n'. But when I try to store them into the list, it is getting stored as letters, not as whole sentences. Below is the code snippet for reference
input = open("input.txt", "r")
lines = input.readlines()
input_ = []
for i in range(len(lines)):
if len(lines[i]) <= 2:
for j in range(i):
input_.append(lines[j]) #This line is storing as letters.
even "input_ += lines" is storing as letters, Not as sentences.
Any idea how to modify this code to get the desired output ?
Don't forgot to do input.close(), or the file won't save.
Alternatively you can use with.
#Using "with" closes the file automatically, so you don't need to write file.close()
with open("input.txt","r") as file:
file_ = file.read().split("\n")
file_ is now a list with each paragraph as a separate item.
It's as simple as 2 lines.

Parse lines into individual segments - python

I'm new to python and having issues working with a text file. The text file structure being used is shown. What I'm trying to do is first split the two polylines into their own variable and then split each variable into individual coordinates. The end goal is to have it structured as:
polyline 1:
[###, ###] [###, ###]
polyline 2:
[###, ###] [###, ###]
Text file structure:
Polyline;
1: ###,###; ###,###
2: ###,###; ###,###; ###,###
The code I've tried is just working with a single line. While I've been able to split the single line, I have not been able to move to the next step which is to split the line further.
f=open('txt.txt', 'r')
pl = []
for line in f.read().split('\n'):
if (line.find('1: ') !=-1):
ln = line.split('1: ')
print ln
f.close()
What is the best way to split the line to the end state?
First of all you can use with ... as statement to open a file which will close the file at the end of block , secondly you don't have to read the file and split with \n just use a for loop to loop over your file object.
Also for checking the start with digit number you can us regex and in this case you can use re.match function, then you can split the line with ; and using a list comprehension split another parts with , :
import re
with open('txt.txt') as f:
for line in f:
if re.match(r'\d:.*',line):
ln = [var.split(',') for var in line.split(';')]
print ln

Categories