I'm a huge python noob. Trying to write simple script that will split a line in a file where it see's a "?"
line in Input file (inputlog.log):
http://website.com/somejunk.jpg?uniqid&=123&an=1234=123
line in output file (outputlog.log):
http://website.com/somejunk.jpg uniqid&=123&an=1234=123
The goal here is to end up with a file that has 2 columns:
Here's my code it kinda works except it wont write to the 2nd file
"TypeError: expected a character buffer object"
import re
a = raw_input("what file do you want to open? ")
b = raw_input("what is the file you want to save to? ")
with open(a, 'r') as f1:
with open(b,'w') as f2:
data = f1.readlines()
print "This is the line: ", data #for testing
for line in data:
words= re.split("[?](.*)$",line)
print "Here is the Split: ", words #for testing
f2.write(words)
f1.close()
f2.close()
Your problem is that 'words' is a list. You cannot write that to your file. You need to convert it back to a string. Also, you need to pay attention when converting it back to make sure that you create the spacing/split you want between the string.
You should do something like this.
words = ' '.join(words)
Pay close attention to the space inside the single quotes. That indicates it will put a space between your strings.
Finally, you then make your call to:
f2.write(words)
Upon making that change, I tested your code and it successfully split and wrote them to the file per your specification.
Related
Image of the text file
Now what I want in this text file is the complete text written in a single line so that it looks like this one:What I want it to be like
How do I do this using python? I tried strip function ,replace function etc. It isnt just working.
You just need to read the file in, remove the new lines and write it again.
with open("./foo.txt", "r") as f:
formatted = ""
for line in f.readlines():
formatted += line.replace('\n', " ") # Removes the new lines, add spaces instead
formatted.replace(" ", " ") # Replace double space with one space
# Writes single line to textfile
with open("./bar.txt", "w") as out:
out.write(formatted)
This is my code. What it should do is open the file called example.txt in the same directory and it should only print out the first word of a big list.
with open('example.txt') as file:
line = 'example.txt'
important_info = line.split()
print(important_info[0])
I'm pretty sure I messed up but I don't know how.
I first coded this and it worked
acc = ('info blah bloh blrjejw bfwe tee')
tui = acc.split()
print(tui[0])
In the code I showed above it only prints the first word for one line. But I want something that can do over 100 lines quickly. T think I'm close.
Want to make sure I understand- you want this program to read the first line of a file and print the first word right?
You're on the right track. You're accidentally splitting on the name of the file rather than it's file contents- you're missing the code that reads the contents of the file.
To explain your code:
with open('example.txt') as file:
line = 'example.txt'
important_info = line.split()
print(important_info[0])
( important_info is a list containing just the filename i.e. ['example.txt'] so printing the first element would just be the string example.txt )
Something like this would work (reading the first line, splitting it by whitespace so that it's a list of words and then printing the first word in that list)
f = open("example.txt", "r")
print(f.readline().split()[0])
You need to read the file:
with open('example.txt') as file:
line = file.read()
important_info = line.split()
print(important_info[0])
I am trying to make a program that reads from a file and deletes one specific line inside of it and then puts all the data stored back to the file separated with a new line. The file uses this format:
Jones|20|20|00
bob|30|19|90
James|40|19|80
So I want to delete (backup contains this and is the line I want to delete)
bob|30|19|90
but the code that I am using takes away the new line and doesnt replace it but when I try to add \n to it the file doesn't want to read as it does this (adds 2 "\n"s):
Jones|20|20|00
James|40|19|80
I am using this code below:
def deleteccsaver(backup):
lockaccount =""
lockaccount = lockaccount.strip("\n")
with open('accounts_project.txt','r+') as f:
newline=[]
for line in f.readlines():
newline.append(line.replace(backup, lockaccount).strip("\n"))
with open('accounts_project.txt','w+') as f:
for line in newline:
f.writelines(line +"\n")
f.close()
resetlogin()
Please help as I dont know how to add the \n back without it appearing as "\n\n"
Without the "\n "it appears as:
Jones|20|20|00James|40|19|80
Any suggestions:
What I am doing here is reading the entire file at once, please don't do this if you have a very very big file. After reading all file contents at once, I am making a list out of it using "\n" as a delimiter. Read about split function in python to know more about it. Then from the list I am replacing the backup with lockaccount, as you have been doing the same, these are the names of variables that you are using, hope I did not confuse between them in this case. Then it will be saved to a new file after adding new line after each element of list, i.e. each line of the previous file. This will cause the result file to have all the contents as previous file, but removing what you wanted to remove. I see that lockaccount is itself an empty string, so adding it might create a newline in your file. In case you dont want lockaccount to replace the backup variable in the file, just remove the backup from the list using contents.remove(backup) instead of contents[contents.index(backup)] == lockaccount keeping the rest of the code same. Hope this explains better.
def deleteccsaver(backup):
lockaccount =""
lockaccount = lockaccount.strip("\n")
with open('accounts_project.txt','r+') as f:
contents = f.read().split("\n")
if backup in contents:
contents[contents.index(backup)] = lockaccount
new_contents = "\n".join(contents)
with open('accounts_project.txt','w+') as f:
f.write(new_contents)
resetlogin()
You are priting a newline character after each element in the list. So, if you replace a line with the empty string, well, you will get an empty line.
Try to simply skip over the line you want to delete:
if line == backup:
contiune
else:
lines.append(...)
PS. There is room for improvment in the code above, but I'm on the phone, I will get back with an edit later if nobody gets ahead of me
You can try to add newline = '\n'.join(newline) after your first for loop and then just write it into the accounts_project.txt file without a loop.
The code should then look like:
def deleteccsaver(backup):
lockaccount =""
lockaccount = lockaccount.strip("\n")
with open('accounts_project.txt','r+') as f:
newline=[]
for line in f.readlines():
newline.append(line.replace(backup, lockaccount).strip("\n"))
newline = '\n'.join(newline)
with open('accounts_project.txt','w+') as f:
f.write(newline)
f.close() # you don't necessarily need it inside a with statement
resetlogin()
Edit:
Above code still results in
Jones|20|20|00
James|40|19|80
as output.
That's because during the replacement loop an empty string will be appended to newline (like newline: ['Jones|20|20|00','','James|40|19|80']) and newline = '\n'.join(newline) will then result in 'Jones|20|20|00\n\nJames|40|19|80'.
A possible fix can be to replace:
for line in f.readlines():
newline.append(line.replace(backup, lockaccount).strip("\n"))
with
for line in f.readlines():
line = line.strip('\n')
if line != backup:
newline.append(line)
def deleteccsaver(backup):
lockaccount =""
lockaccount = lockaccount.strip("\n")
with open('accounts_project.txt','r+') as f:
contents = f.read().split("\n")
if backup in contents:
contents.remove(backup)
new_contents = "\n".join(contents)
with open('accounts_project.txt','w+') as f:
f.write(new_contents)
resetlogin()
I need to write a program which will store a list of words that the user has input & a list of their positions .
This can be saved as easier one file or two files.
In this piece of code I've only saved it into one blank text file although on further rethink of this , programming wise maybe it would be easier to save them separately as two different files.
I've tested my code and know that the program will take the user's input and output the positions but the part I'm struggling with is the file handling of it and saving it to the file as this outputs an error. Is there any helpful websites to help me solve this problem or some useful functions/ modifications?
Thanks.
Here is my code:
#SUBROUTINES
def saveItem():
#save an item into a new file
print("creating a text file with the write() method")
textfile=open("task2.txt","w")
for item in words:
textfile.write(positions)
textfile.write("\n")
textfile.close()
print("The file has been added!")
#mainprogram
sentence = input("Write your sentence here ")
words = sentence.split()
positions = [words.index(word) + 1 for word in words]
print (sentence)
print (positions)
saveItem()
#filehandling
file=open("task2.txt", "r" )
#opens a file called "filename.txt" for "reading"
contents = file.read()
#reads everything in the file into a string called 'contents'
file.close()
print(contents)
#we have finished with the file now.
a=True
while a:
print("Press 1 to save the file:\n\
1.Save?\n\:")
z=int(input())
if z == 1:
saveItem()
else:
print("incorrect option")
Here is the error python gives:
Traceback (most recent call last):
File "C:task2.3.py", line 21, in
saveItem()
File "C:task2.3.py", line 7, in saveItem
textfile.write(positions)
TypeError: must be str, not list
I tried your code and I got 2 errors
First is: unexpected EOF while parsing
solved by changing input to raw_input in this line:
sentence = raw_input("Write your sentence here ")
you may refer to this: Python unexpected EOF while parsing
Second: expected a character buffer object
solved by converting positions to string like this:
textfile.write(str(positions))
you may refer to this also: TypeError: expected a character buffer object
That worked with me. Plus, I believe you might want to remove the (for item in words) loop cause it just repeats writing all of the positions for every word.
It sounds like you just want a string representation of your positions list. The easiest way to do that is to just cast the list to a str
with open("task2.txt","w") as textfile:
textfile.write(str(positions))
The Problem - Update:
I could get the script to print out but had a hard time trying to figure out a way to put the stdout into a file instead of on a screen. the below script worked on printing results to the screen. I posted the solution right after this code, scroll to the [ solution ] at the bottom.
First post:
I'm using Python 2.7.3. I am trying to extract the last words of a text file after the colon (:) and write them into another txt file. So far I am able to print the results on the screen and it works perfectly, but when I try to write the results to a new file it gives me str has no attribute write/writeline. Here it the code snippet:
# the txt file I'm trying to extract last words from and write strings into a file
#Hello:there:buddy
#How:areyou:doing
#I:amFine:thanks
#thats:good:I:guess
x = raw_input("Enter the full path + file name + file extension you wish to use: ")
def ripple(x):
with open(x) as file:
for line in file:
for word in line.split():
if ':' in word:
try:
print word.split(':')[-1]
except (IndexError):
pass
ripple(x)
The code above works perfectly when printing to the screen. However I have spent hours reading Python's documentation and can't seem to find a way to have the results written to a file. I know how to open a file and write to it with writeline, readline, etc, but it doesn't seem to work with strings.
Any suggestions on how to achieve this?
PS: I didn't add the code that caused the write error, because I figured this would be easier to look at.
End of First Post
The Solution - Update:
Managed to get python to extract and save it into another file with the code below.
The Code:
inputFile = open ('c:/folder/Thefile.txt', 'r')
outputFile = open ('c:/folder/ExtractedFile.txt', 'w')
tempStore = outputFile
for line in inputFile:
for word in line.split():
if ':' in word:
splitting = word.split(':')[-1]
tempStore.writelines(splitting +'\n')
print splitting
inputFile.close()
outputFile.close()
Update:
checkout droogans code over mine, it was more efficient.
Try this:
with open('workfile', 'w') as f:
f.write(word.split(':')[-1] + '\n')
If you really want to use the print method, you can:
from __future__ import print_function
print("hi there", file=f)
according to Correct way to write line to file in Python. You should add the __future__ import if you are using python 2, if you are using python 3 it's already there.
I think your question is good, and when you're done, you should head over to code review and get your code looked at for other things I've noticed:
# the txt file I'm trying to extract last words from and write strings into a file
#Hello:there:buddy
#How:areyou:doing
#I:amFine:thanks
#thats:good:I:guess
First off, thanks for putting example file contents at the top of your question.
x = raw_input("Enter the full path + file name + file extension you wish to use: ")
I don't think this part is neccessary. You can just create a better parameter for ripple than x. I think file_loc is a pretty standard one.
def ripple(x):
with open(x) as file:
With open, you are able to mark the operation happening to the file. I also like to name my file object according to its job. In other words, with open(file_loc, 'r') as r: reminds me that r.foo is going to be my file that is being read from.
for line in file:
for word in line.split():
if ':' in word:
First off, your for word in line.split() statement does nothing but put the "Hello:there:buddy" string into a list: ["Hello:there:buddy"]. A better idea would be to pass split an argument, which does more or less what you're trying to do here. For example, "Hello:there:buddy".split(":") would output ['Hello', 'there', 'buddy'], making your search for colons an accomplished task.
try:
print word.split(':')[-1]
except (IndexError):
pass
Another advantage is that you won't need to check for an IndexError, since you'll have, at least, an empty string, which when split, comes back as an empty string. In other words, it'll write nothing for that line.
ripple(x)
For ripple(x), you would instead call ripple('/home/user/sometext.txt').
So, try looking over this, and explore code review. There's a guy named Winston who does really awesome work with Python and self-described newbies. I always pick up new tricks from that guy.
Here is my take on it, re-written out:
import os #for renaming the output file
def ripple(file_loc='/typical/location/while/developing.txt'):
outfile = "output.".join(os.path.basename(file_loc).split('.'))
with open(outfile, 'w') as w:
lines = open(file_loc, 'r').readlines() #everything is one giant list
w.write('\n'.join([line.split(':')[-1] for line in lines]))
ripple()
Try breaking this down, line by line, and changing things around. It's pretty condensed, but once you pick up comprehensions and using lists, it'll be more natural to read code this way.
You are trying to call .write() on a string object.
You either got your arguments mixed up (you'll need to call fileobject.write(yourdata), not yourdata.write(fileobject)) or you accidentally re-used the same variable for both your open destination file object and storing a string.