Bringing in a text file that is formatted like below:
hammer#9.95
saw#20.15
shovel#35.40
I need to bring it into python and format it so that it is in line with an existing snippet of code:
# display header line for items list
print('{0: <10}'.format('Item'), '{0: >17}'.format('Cost'), sep = '' )
The goal is for the text file to be in line with existing headers like so:
Item Cost
hammer $9.95
saw $20.15
shovel $35.4
I can bring in the text file into Python and get replace the # sign with a $ sign:
file = open('Invoice.txt', 'r')
file_contents = file.read()
new_file_contents = file_contents.replace('#', '$')
Which gives me this output:
hammer$9.95
saw$20.15
shovel$35.40
but I'm having trouble with the formatting aspect. Any suggestions?
You can do something like this:
with open(file,'rt',encoding='utf-8') as infile:
for line in infile:
print("{:<6} {}".format(line.strip().split('#')[0],"$"+line.strip().split("#")[1]))
only problem is that it'll look ugly if you have a longer word than hammer. I suggest finding the largest word in your list first, then using that as the limiter for the {:<6}.
Related
I have a large text file I have imported in python and want to split into lines by a key word, then use those lines to take out relevent information into a dataframe.
The data follows along the same pattern for each line but wont be the exact same number of characters and some lines may have extra data
So I have a text file such as:
{data: name:Mary, friends:2, cookies:10, chairs:4},{data: name:Gerald friends:2, cookies:10, chairs:4, outside:4},{data: name:Tom, friends:2, cookies:10, chairs:4, stools:1}
There is always the key word data between lines, is there any way I can split it out by using this word as the beginning of the line (then put it into a dataframe)?
I'm not sure where to begin so any help would be amazing
When you get the content of a .txt file like this...
with open("file.txt", 'r') as file:
content = file.read()
...you have it as a string, so you can split it with the function str.split():
content = content.split(my_keyword)
You can do it with a function:
def splitter(path: str, keyword: str) -> str:
with open(path, 'r') as file:
content = file.read()
return content.split(keyword)
that you can call this way:
>>> splitter("file.txt", "data")
["I really like to write the word ", ", because I think it has a lot of meaning."]
I am currently trying to write a script that reads a text file line by line, and transfers all lines with an occurrences of a specific str, for example (if the line has the string 'apple' or 'Hello World'), to a new text file.
fpath = open('Redshift_twb.txt', 'r')
lines = fpath.readlines()
fpath_write = open('Redshift_new.txt', 'w+')
print(lines[0:10])
fpath_write.write(lines[0:10])
fpath.close()
fpath_write.close()
Currently, I just have the basics set up, but I am stuck because when I use .readlines() it adds all the lines to a list at separate indexes. Then when I try to write back to a new file, I get an error message:
fpath_write.write(lines[0:10])
TypeError: write() argument must be str, not list
Because you cannot use the function .write() on a list.
I know this is a two part question, but any help at all would be greatly appreciated.
Read the lines in the input file
Filter the list to find the lines that you need to write to the output file
join whose lines with \n - to stitch together a single string
Write that string to the output file
fpath = open('Redshift_twb.txt', 'r')
lines = fpath.readlines()
fpath_write = open('Redshift_new.txt', 'w+')
print(lines[0:10])
# filter the list; with the string 'apple'
# replace 'apple' with whatever string you want to find
out_lines = [line for line in lines if 'apple' in line]
# Join the lines into a single string
output = '\n'.join(out_lines)
# write it
fpath_write.write(output)
fpath.close()
fpath_write.close()
Replace your fpath_write.write(lines[0:10]) with this fpath_write.write(''.join(line for line in lines[0:10] if 'content1' in line))
I am trying to extract data from a .txt file in Python. My goal is to capture the last occurrence of a certain word and show the next line, so I do a reverse () of the text and read from behind. In this case, I search for the word 'MEC', and show the next line, but I capture all occurrences of the word, not the first.
Any idea what I need to do?
Thanks!
This is what my code looks like:
import re
from file_read_backwards import FileReadBackwards
with FileReadBackwards("camdex.txt", encoding="utf-8") as file:
for l in file:
lines = l
while line:
if re.match('MEC', line):
x = (file.readline())
x2 = (x.strip('\n'))
print(x2)
break
line = file.readline()
The txt file contains this:
MEC
29/35
MEC
28,29/35
And with my code print this output:
28,29/35
29/35
And my objetive is print only this:
28,29/35
This will give you the result as well. Loop through lines, add the matching lines to an array. Then print the last element.
import re
with open("data\camdex.txt", encoding="utf-8") as file:
result = []
for line in file:
if re.match('MEC', line):
x = file.readline()
result.append(x.strip('\n'))
print(result[-1])
Get rid of the extra imports and overhead. Read your file normally, remembering the last line that qualifies.
with ("camdex.txt", encoding="utf-8") as file:
for line in file:
if line.startswith("MEC"):
last = line
print(last[4:-1]) # "4" gets rid of "MEC "; "-1" stops just before the line feed.
If the file is very large, then reading backwards makes sense -- seeking to the end and backing up will be faster than reading to the end.
Suppose I have a file like this:
words
words
3245, 3445,
345634, 345678
I am wondering if it is possible to add data onto the 4th line of the code so the out put is this:
words
words
3245, 3445, 67899
345634, 345678
I found a similar tutorial: appending a data in a specific line of a text file in Python?
but the problem is I don't want to use .startswith because the files will all have different beginnings.
Thanks for your help!
You can achieve that by doing this
# define a function so you can re-use it for writing to other specific lines
def writetoendofline(lines, line_no, append_txt):
lines[line_no] = lines[line_no].replace('\n', '') + append_txt + '\n'
# open the file in read mode to read the current input to memory
with open('./text', 'r') as txtfile:
lines = txtfile.readlines()
# in your case, write to line number 4 (remember, index is 3 for 4th line)
writetoendofline(lines, 3, ' 67899')
# write the edited content back to the file
with open('./text', 'w') as txtfile:
txtfile.writelines(lines)
# close the file
txtfile.close()
My text.txt looks like this
abcd
xyzv
dead-hosts
-abcd.srini.com
-asdsfcd.srini.com
And I want to insert few lines after "dead-hosts" line, I made a script to add lines to file, there is extra space before last line, that's mandatory in my file, but post added new lines that space got removed, dont know how to maintain the space as it is.
Here is my script
Failvrlist = ['srini.com','srini1.com']
tmplst = []
with open(‘test.txt’,'r+') as fd:
for line in fd:
tmplst.append(line.strip())
pos = tmplst.index('dead-hosts:')
tmplst.insert(pos+1,"#extra comment ")
for i in range(len(Failvrlist)):
tmplst.insert(pos+2+i," - "+Failvrlist[i])
tmplst.insert(pos+len(Failvrlist)+2,"\n")
for i in xrange(len(tmplst)):
fd.write("%s\n" %(tmplst[i]))
output is as below
abcd
xyzv
dead-hosts
#extra comment
- srini.com
- srini1.com
- abcd.srini.com
- asdsfcd.srini.com
if you look at the last two lines the space got removed, please advise .
Points:
In you code , pos = tmplst.index('dead-hosts:'), you are trying to find dead-hosts:. However, input file you have given has only "dead hosts". No colon after dead-hosts, I am considering dead-hosts:
While reading file first time into list, use rstrip() instead of strip(). Using rstrip() will keep spaces at the start of line as it is.
Once you read file into list, code after that should be outside with block which is use to open and read file.
Actually, flow of code should be
Open file and read lines to list and close the file.
Modify list by inserting values at specific index.
Write the file again.
Code:
Failvrlist = ['srini.com','srini1.com']
tmplst = []
#Open file and read it
with open('result.txt','r+') as fd:
for line in fd:
tmplst.append(line.rstrip())
#Modify list
pos = tmplst.index('dead-hosts:')
tmplst.insert(pos+1,"#extra comment")
pos = tmplst.index('#extra comment')
a = 1
for i in Failvrlist:
to_add = " -" + i
tmplst.insert(pos+a,to_add)
a+=1
#Write to file
with open('result.txt','w') as fd:
for i in range(len(tmplst)):
fd.write("%s\n" %(tmplst[i]))
Content of result.txt:
abcd
xyzv
dead-hosts:
#extra comment
-srini.com
-srini1.com
-abcd.srini.com
-asdsfcd.srini.com