Read file by removing the unwanted lines using python pandas - python-2.7

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"}}]

Related

How to read a file line by line in crystal language?

Hi I want to read a file line by line with crystal language, but I don't know how can I do that.
I read crystal documentation, but I couldn't find my answer.
It's my code:
system("ls /etc/NetworkManager/system-connections/ > Fox.txt")
file = File.read("Fox.txt")
system("sudo cat /etc/NetworkManager/system-connections/\'#{file}\' >> Fox_done.txt")
To read a file line by line, you can use File#each_line:
File.each_line("/path/to/input.txt") do |line|
puts line
end
If the file is small and you want to load all lines in memory, you can also use File#read_lines:
File.read_lines("/path/to/input.txt") # returns a Array(String)

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

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?

Python: only run command once in for loop

I have a for loop which creates a CSV of values of several files in a directory.
Within this loop I only want to create the file and write in the header once, currently I am doing this:
#name&path to table file
test = tablefile+"/"+str(cell[:-10])+"_Table.csv"
#write file
if not os.path.isfile(test):
csv.writer(open(test, "wt"))
with open(test, 'w') as output:
wr = csv.writer(output, lineterminator=',')
for val in header_note:
wr.writerow([val])
and to append data I have:
with open(test, 'a') as output:
wr = csv.writer(output, lineterminator=',')
for val in table_all:
wr.writerow([val])
Which works well, however, when I run the script over again another time it will append more data to the bottom of that same .csv. What I want is for the first time through the for-loop, is to just overwrite any existing .csv with a new one with a header then continue on appending data, and overwrite/re-write header once the script is run again. Thanks!
It look like you may have some code problems other than file handling, but here goes: You problem is basically that opening a file in 'w' mode will overwrite everything in the file, and opening in 'a' mode will not allow you to change the header line.
To get around this, you will have to get the contents of the file (if it already exists), then overwrite the file, including those lines that where there to begin with.
You will want something along the lines of:
if os.path.exists(file_name): # if file already exists
with open(file_name, 'r') as in_file: # open it
old_lines = in_file.readlines()[1:] # read all lines from file EXCEPT header line
with open(file_name, 'w') as out_file: # open file again, with 'w' to create/overwrite
out_file.write(new_header_line) # write new header line to file
for line in old_lines:
out_file.write(line) # write all preexisting lines back into file
# continue writing whatever you want.

Formatting text file in Python

I want to format an existing text file, the contents of text file are:
Aurangabad
Adilabad
Beed
I want to format it like:
Aurangabad|Aurangabad,
Adilabad|Adilabad,
Beed|Beed,
I am not so good in Python file handling.
the code to do so:
with open('file_name.txt','r') as file:
list_of_lines = file.readlines()
new_lines_list = []
for line in list_of_lines:
line = line.replace('\n','') #because each line end with this and we don't need it now (\n is the newline chr)
new_lines_list.append('{0}|{0}\n'.format(line)) #the same as - new_lines_list.append(line+'|'+line+'\n')
with open('file_name.txt','w') as file:
string_to_write = ''.join(new_lines_list)
file.write(string_to_write)
if you don't understand the with statement: it is basically to open the file and at the end it will close itself (and even if some exception occur it will still close (I explain bad if you don't understand go here)

Python: copy line, conditional criteria

I have been searching for following Python solution to copy selectively lines from 1 txt file to another. I can copy the whole file, but with only a few lines I get an error.
My code:
f = open(from_file, "r")
g = open(to_file, "w")
#copy = open(to_file, "w") # this instruction copies whole file
rowcond2 = 'xxxx' # look for this string sequence in every line
for line in f:
if rowcond2 in f:
copy.write(line,"w") in g # write every corresponding line to destination
f.close()
# copy.close() # code receive error to close destination
g.close()
So without the rowcond2, I can copy the whole file. Yet with the condition nothing is written to destination file.
Thank you for your help.
Why not to put your condition inside the for loop?
for line in f:
if condition:
copy.write(line)
I have been able to solve this case searching on SO:
Using python to write specific lines from one file to another file
#Lukas Graf: thank you for your detailed step wise explanation.