Unable to write text inside a file using with open r+ PYTHON - regex

I am using Python 2.7.
I want to create a script to scan the text file for specific keywords like want to test and write or replace string (b3). My script:
#! usr/bin/python
import re
from os.path import abspath,exists
open_file = abspath("zzwrite.txt")
if exists(open_file):
with open(open_file,"r+") as write1:
for line in write1:
matching = re.match(r'.* want to test (..)',line,re.I)
if matching:
print ("Done matching")
write1.write("Success")
print >> write1,"HALELUJAH"
My input text file:
I just want to read 432
I just want to write 213
I just want to test b3 experiment
I just want to sleep for 4 hours
I managed to complete matching as there is a print "done matching" to indicates the codes are able to execute the last 'if' condition but no single string is written or replaced inside the text file. The string "b3" is different in every input text file so I do not prefer using str.replace("b3", "xx") method. Is there anything I missing in the script?

Related

Read file by removing the unwanted lines using python pandas

I am reading a file which contains json data and in between it contains other text.So for that i want to check that condition on reading the file if line starts with condition how can i achieve this?
with open ("inputfile.txt") as f:
content = f.read().replace('}U','},')[::-1].replace(',', '', 1)].replace(":[",":").replace("]","")
content = '[{}]'.format(content)
data=json.loads(content)
I want to check the file if the line starts with condition like this
startswith("{"+"\"M\""+":")
I Have tried reading line by line and checking if the line startswith condition but for large files it is tak
inputfile.txt
sometext
{"M":{"1":"data","2":"data2"}}U
asdklaasd
{"M":{"3":"555","5":"3333"}}U
I want to read the lines only that start with {"M":
Output I need is like this
[{"M":{"1":"data","2":"data2"}},{"M":{"3":"555","5":"3333"}}]

regular expression Pattern object getting converted to string if fetched from config.cfg file

python file
import ConfigParser,re
config=ConfigParser.ConfigParser()
with open("temp.cfg",'r') as config_file:
config.readfp(config_file)
x=[]
x.append(re.compile(r'abc'))
x.append((config.get("ssp",'a')).strip('"'))
print x[0]
print x[1]
config file[temp.cfg]
[ssp]
a:re.compile(r'abc')
output
>>>
<_sre.SRE_Pattern object at 0x02110F80>
re.compile(r'abc')
>>>
What "print x[1]" should give is regular expression object but it seems to be returning string.
Looks like I am not doing it in the right way & am unable to figure it out
The output of x[1] is because of the following:
x.append((config.get("ssp",'a')).strip('"'))
Since, config is the cfg file parser object, you are accessing the option a of ssp section:
[ssp]
a:re.compile(r'')
which is obviously, the string: re.compile(r'').
Using eval:
x.append(eval((config.get("ssp",'a')).strip('"')))
You need to change your config file to this:
[ssp]
a:abc
Then you can simply do (without ugly eval())
x.append(re.compile(config.get("ssp", "a")).strip())
You read the regex string first from config file and then translate it to a regex object in your code with re.compile() instead of eval(). That's also better for maintainance, because you save a lot of text in the config file (a:abc instead of a:re.compile(r'abc') for each line).

How to get a file to be used as input of the program that ends with special character in python

I have an output file from a code which its name will ends to "_x.txt" and I want to connect two codes which second code will use this file as an input and will add more data into it. Finally, it will ends into "blabla_x_f.txt"
I am trying to work it out as below, but seems it is not correct and I could not solve it. Please help:
inf = str(raw_input(*+"_x.txt"))
with open(inf+'_x.txt') as fin, open(inf+'_x_f.txt','w') as fout:
....(other operations)
The main problem is that the "blabla" part of the file could change to any thing every time and will be random strings, so the code needs to be flexible and just search for whatever ends with "_x.txt".
Have a look at Python's glob module:
import glob
files = glob.glob('*_x.txt')
gives you a list of all files ending in _x.txt. Continue with
for path in files:
newpath = path[:-4] + '_f.txt'
with open(path) as in:
with open(newpath, 'w') as out:
# do something

Beginner, python - how to read a list from a file

I have a Word document that is literally a list of lists, that is 8 pages long. Eg:
[['WTCS','Dec 21'],['THWD','Mar 22']...]
I am using Linux Mint, Python 3.2 and the IDLE interface, plus my own .py programs. I need to read and reference this list frequently and when I stored it inside .py programs it seemed to slow down the window considerably as I was editing code. How can I store this information in a separate file and read it into python? I have it in a .txt file now and tried the following code:
def readlist():
f = open(r'/home/file.txt','r')
info = list(f.read())
return(info)
but I get each character as an element of a list. I also tried info = f.read() but I get a string. Thanks!
You can convert a Python list read from a text file from a text file as a string into a list using the ast module:
>>> import ast
>>> s = "[['WTCS','Dec 21'],['THWD','Mar 22']]"
>>> ast.literal_eval(s)
[['WTCS', 'Dec 21'], ['THWD', 'Mar 22']]

Formatting text file

I have a txt file that I would like to alter so I will be able to place the data into columns see example below. The reason behind this is so I can import this data into a database / array and perform calculations on them. I tried importing/pasting the data into LibreCalc but it just imports everything into one column or it opens the file in LibreWriter I'm using ubuntu 10.04. Any ideas? I'm willing to use another program to work around this issue. I could also work with a comma delimited file but I'm not to sure how to convert the data to that format automatically.
Trying to get this:
WAVELENGTH, WAVENUMBER, INTENSITY, CLASSIFICATION, CODE,
1132.8322, 88274.326, 2300, PT II, 9356- 97630, 05,
Here's a link to the full file.
pt.txt file
Try this:
sed -e "s/(\s+)/,$1/g" pt.txt
is this what you want?
awk 'BEGIN{OFS=","}NF>1{$1=$1;print}' pt.txt
if you want the output format looks better, and you have "column" installed, you can try this too:
awk 'BEGIN{OFS=", "}NF>1{$1=$1;print}' pt.txt|column -t
The awk and sed one-liners are cool, but I expect you'll end up needing to do more than simply splitting up the file. If you do, and if you have access to Python 2.7, the following little script will get you going.
# -*- coding: utf-8 -*-
"""Convert to comma-delimited"""
import csv
from os import path
import re
import sys
def splitline(line):
return re.split('\s{2,}', line)
def main():
srcpath = path.abspath(sys.argv[1])
targetpath = path.splitext(srcpath)[0] + '.csv'
with open(srcpath) as infile, open(targetpath, 'w') as outfile:
writer = csv.writer(outfile)
for line in infile:
if line.startswith(' '):
line = line.strip()
cols = splitline(line)
writer.writerow(cols)
if __name__ == '__main__':
main()
The easiest way turned out to be importing using a fixed width like tohuwawohu suggested
Thanks
Without transforming it to a comma-separated file, you could access the csv import options by simply changing the file extension to .csv (maybe you should remove the "header" part manually, so that only the columns heads and the data rows do remain). After that, you can try to use whitespace as column delimiter, or even easier: select "fixed width" and set the columns manually. – tohuwawohu Oct 20 at 9:23