Typeerror: Expected a Character Buffer Object - While Trying to Save Integer to Textfile

TypeError: expected a character buffer object

Assuming you just want to write the string '649' to the file, change row to '649' or issue f.write(str(row)).

TypeError: expected a character buffer object - while trying to save integer to textfile

Have you checked the docstring of write()? It says:

write(str) -> None. Write string str to file.

Note that due to buffering, flush() or close() may be needed before
the file on disk reflects the data written.

So you need to convert y to str first.

Also note that the string will be written at the current position which will be at the end of the file, because you'll already have read the old value. Use f.seek(0) to get to the beginning of the file.`

Edit: As for the IOError, this issue seems related. A cite from there:

For the modes where both read and writing (or appending) are allowed
(those which include a "+" sign), the stream should be flushed (fflush)
or repositioned (fseek, fsetpos, rewind) between either a reading
operation followed by a writing operation or a writing operation
followed by a reading operation.

So, I suggest you try f.seek(0) and maybe the problem goes away.

TypeError: expected a character buffer object

What the error message is saying is that you can't write a list to a file, only "a character buffer object", meaning a string or something else that acts a lot like a string.

If you just want to write the list to the file in the same way you'd print them to the console, you can write str(thefile) or repr(thefile) (or even use the redirect syntax in print instead of using file.write).

But you're using the csv module to read the input, and presumably want the output in the same format, so you probably want to use csv to write it too.

You're reading like this:

list(csv.reader(open(filename, 'rb'), delimiter=',', quotechar='"'))[1:]

So write like this:

csv.writer(open('foo.csv', 'wb'), delimiter=',', quotechar='"').writerows(thefile)

I should mention that I wouldn't structure the code like this in the first place; I'd do something like this:

with open('input.csv', 'rb') as infile, open('output.csv', 'wb') as outfile:
incsv = csv.reader(infile, delimiter=',', quotechar='"')
outcsv = csv.writer(outfile, delimiter=',', quotechar='"')
incsv.read() # skip first line
for line in incsv:
if line[3] != '':
outcsv.write(ProcessLine(line))

Python - TypeError: expected a character buffer object

You're trying to write a dictionary object to a text file, where as the function is expecting to get some characters to write. If you want your object to be stored in a text format that you can read, you need some way to structure your data, such as JSON.

import json
with open('playlist.json', 'w') as f:
json.dump(playlist, f)

There are other options such as xml or maybe even csv. If you don't care about your data being in a plain text readable format, you could also look at pickling the dictionary object.

As noted in the comments, your question appended data to a file, rather than writing a new file. This is an issue for JSON as the hierarchical structure doesn't work when its appended too. If you need to add to an existing file you may need to come up with a different structure for your stored text, read the exiting file, combine it with the new data and rewrite it (Jack Hughes answer)... or you could write some code to parse appended JSON, but I guess that's not the point of standards.

TypeError: expected a character buffer object

You are not returning anything getPDFContent() so basically you are writing None.

 result=[]
for i in range(0, pdf.getNumPages()):
result.append(pdf.getPage(i).extractText().encode("ascii", "ignore")) # store all in a list
return result

s = getPDFContent()
with open('D:\pdftxt.txt', 'w') as pdftxt:
pdftxt.writelines(s) # use writelines to write list content

How your code should look:

def getPDFContent():
# Load PDF into pyPDF
pdf = pyPdf.PdfFileReader(file("D:\output.pdf", "rb"))
# Iterate pages
result = []
for i in range(0, pdf.getNumPages()):
result.append(pdf.getPage(i).extractText().encode("ascii", "ignore"))
return result

s = getPDFContent()
with open('D:\pdftxt.txt', 'w') as pdftxt:
pdftxt.writelines(s)

TypeError: expected a character buffer object for loop and while loop function

One way or another, you can only write character buffers to python File objects. And python won't include newlines by default when you write to a file, you have to include those yourselves.

I'll also recommend using a context manager for your File object.

See this code:

with open("results.txt", 'w') as output:
for i in range(100):
population = range(0, 100)

# TTF = 0 # Nothing is done with this number. Why is it here?

while len(set(population)) != 1:
scalingfactor = np.random.poisson(5, 100)
sample = np.repeat(population, scalingfactor)
decrease-to-ten = np.random.choice(sample, 100)
population = decrease-to-ten
results += 1
output.write(f"{results}\n") # Assuming you're using Python >= 3.6

If you're on an older version of Python that doesn't support f-string, replace f"{results}\n" with "{0}\n".format(results)

Python expected a character buffer object

start and end are integers not strings ... you cannot split a string on an integer

"some5string".split(5) # wont work it needs a string
"some5string".split("5") # will work

change it to

print((s.split(str(start)))[1].split(str(end))[0])

Extract single div from HTML and save in place. TypeError: expected a character buffer object

You should either

import bs4
...
soup = bs4.BeautifulSoup(content)

or

from bs4 import BeautifulSoup
...
soup = BeatifulSoup(content)

The issue is caused by attempting to run code written from BeautifulSoup 3 imported as BeautifulSoup on BeautifulSoup 4. Both modules contain a function called BeautifulSoup(), and it is that function your code should call.

expected a character buffer object: Python 2.7

str.split() takes, as its first argument, a string to split the input string on. You are trying to pass it an integer instead.

If you wanted to split on whitespace and pick out a specific element from the result, then index the returned list:

third_element = test.split()[2]

where Python uses 0-based indexing, so the third element is accessed by using 2 as the index. Note that this returns the value, so you'll probably want to store that result.



Related Topics



Leave a reply



Submit