How to import string extracted from a file in python? - python

I have one issue over here in file importing data from text file using python.
I have data like this in my file.
{1:F05ABCDRPRAXXX0000000000}{2:I1230AGRIXXPRXXXXN}{4:
:20:1234567980
:25:AB123465789013246578900000000000
:28c:110/1123156
-}
So from above data I want to fetch data after {4: and line by line like first line is :20:1234567980 and so on.
I want to split data using regular expression So if any python expert have idea how make regular expression for this so provide in answer it will help.
Thank you

If you want to get the lines in a file use
lines = list()
with open("yourfiile.txt") as f:
for line in f:
lines.append(line)
lines.pop(0) #remove the first line (which ends with "{4:")
#do what you want with list of lines

Related

Python 3: Pulling specific data from documents

I am new to python and using it for my internship. My goal is to pull specific data from about 100 .ls documents (all in the same folder) and then write it to another .txt file and from there import it into excel. My problem is I can read all the files, but cannot figure out how to pull the specifics from that file into a list. From the list I want to write them into a .txt file and then import to excel.
Is there anyway to read set readlines() to only capture certain lines?
It's hard to know exactly what you want without an example or sample code/content. What you might do is create a list and append the desired line to it.
result_list = [] # Create an empty list
with open("myfile.txt", "r") as f:
Lines = f.readlines() # read the lines of the file
for line in Lines: # loop through the lines
if "desired_string" in line:
result_list.append(line) # if the line contains the string, the line is added

is it more efficient to read a big file with multiple lines into a string, or directly read the lines from file one a time?

I have a text file with 7000 lines of text. (which may not be much in the grand scheme of what our computer can do...). I want to parse the lines into an array.
Just wondering if it would be more memory efficient to directly work with each line of the file directly into the array, or read the entire file into a string, and simply split the string?
example 1:
text_file_string = open("big_file.txt", "r")
for line in text_file.split('\n'):
array.append(line)
example 2:
with open("big_file.txt","r") as file:
list = file.readlines()
Thank you!

Iterating in a mathematical Sequence

I have this raw data set in a text file where each line is a new piece of data, I need to iterate through the file line by line and change the lines that are dates to a specific date format. Theses dates occur on lines 2, 7, 16, 23 etc
In order to do this I need to iterate over those specific lines so as not to corrupt the data that is on the other lines.
Would there be any way to iterate this way in python?
here is a screencap of the data..
You can see the lines i want manipulated can be found at lines 2, 9, 16, 23 etc
The dates ranges are in the format of Month/Day - Month/Day in case ye have any difficulty finding.
And I will also include the raw text too
It can be found at this link
Link to raw data
#my rough idea
infile = open("polling_Data.txt", "W+")
for line in infile: # specified range
#code to edit date etc
Let me know if ye have any relevant solutions i know that maybe some form of regex may be suitable, open to all sorts of ideas thanks!
I really suggest writing in a new file and then delete the old file just in case anything went wrong along the way. You can do that using the following code:
import re
month_day_regex = r"(\d{,2}/\d{,2} - \d{,2}/\d{,2})"
new_data = []
# reading "polling_Data" text file
with open("polling_Data.txt", "r") as infile:
for line in infile.readlines():
line = line.strip()
if re.match(month_day_regex, line):
new_data.append("##########") #do whatever you want
else:
new_data.append(line)
Now, the variable new_data has the same data as the text file with one change which is replacing the Month/Day entries with ######### just to stand out. Now, let's write this variable into a new file:
with open("new_polling_data.txt", "w") as outfile:
for line in new_data:
outfile.write(line+'\n')
And here is a screenshot of the new file
Try \b\d\d?/\d\d?[ ]?-[ ]?\d\d?/\d\d?\b
demo

I am able to read the txt line by line, buy not sure how can I now search and replace perticular string with X

I am currently trying to develop a python script to sanitize configuration. My objective is to read line by line from txt, which I could using following code
fh = open('test.txt')
for line in fh:
print(line)
fh.close()
output came up as follows
hostname
198.168.1.1
198.168.1.2
snmp string abck
Now I want to
Search the string matching "hostname" replace with X
Search the ipv4 addresses using regular expression
\b(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(.(?1)){3}\b and replace with X.X\1 (replacing only first two octets with X)
Aything after "snmp string" need to replaced with X
so the file final output I am looking for is
X
x.x.1.1
x.x.1.2
snmp string x
I could not orchestrate everything together. Any help or guidance will be greatly appreciated.
There are lots of approaches to this, but here's one: rather than just printing each line of the file, store each line in a list:
with open("test.txt") as fh:
contents = []
for line in fh:
contents.append(line)
print(contents)
Now you can loop through that list in order to perform your regex operations. I'm not going to write that code for you, but you can use python's inbuilt regex library.

How can I go through a dictionary of line separated words one word at a time in python?

I have a dictionary of 10,000 words that I would like to be able to go through one at a time using a for loop. In the dictionary .txt file, each word is on a new line. How would I import the .txt file and isolate each word to perform operations on?
I would suggest starting with something like this:
# open the text file and iterate line by line
with open(myTextFile) as f:
for line in f:
# do something with the content in each line

Categories

Resources