-7

I wanted to add extra functionality to wiki-bot by adding a way for it to write to file and print it at the same time so that the user can skip the long wait to copy all the information. My code works with

print

but not with

f = open("output_from_wikibot.txt", "w")
f.write("Page content:\n", content, "\n")
f.write("Page title:", title, "\n")
f.write("Categories:", categories, "\n")
f.write("Links:", links, "\n")
f.write("References:", references, "\n")
f.write("Summary:", summary, "\n")

It gives me the following error:

TypeError: can only concatenate str (not "list") to str

At first I thought "Oh that's easy, just take each one of them and put them in a different line" but of course, it did not work.

I am newbie to Python and Wikipedia API. I have done projects with PRAW, Discord.py, BeautifulSoup4

3 Answers3

3

As the error message says clearly, write() only takes one argument; it's not like print(), which allows you to give multiple arguments and they'll all be printed. If you want to write multiple strings in a single call, you have to concatenate them.

f.write("Page content:\n" + content + "\n")

or use string formatting:

f.write(f"Page content\n{content}\n")
Barmar
  • 596,455
  • 48
  • 393
  • 495
  • Nope dosent work ``` Traceback (most recent call last): File "main.py", line 38, in f.write(f"Page content\n{content}\n") File "C:\Users\Palash\anaconda3\lib\encodings\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\u0101' in position 30: character maps to ``` – FastAndCurious Oct 01 '20 at 07:32
  • 1
    See https://stackoverflow.com/questions/14630288/unicodeencodeerror-charmap-codec-cant-encode-character-maps-to-undefined – Barmar Oct 01 '20 at 07:37
  • If that doesn't help, you can find more similar posts by googling "python unicodeencodeerror charmap codec" – Barmar Oct 01 '20 at 07:38
  • Saw that , did not think you i could implement it – FastAndCurious Oct 01 '20 at 07:38
  • Try: `f = open("output_from_wikibot.txt", "w", encoding="utf-8")` – Barmar Oct 01 '20 at 07:40
  • with open("output_from_wikibot.txt", "w" , encoding='utf-8') as f: f.write("Page content:\n" + content + "\n") f.write("Page title:" + title + "\n") f.write("Categories:" + categories + "\n") f.write("Links:" + links + "\n") f.write("References:" + references + "\n") f.write("Summary:" + summary + "\n") Worked Now i get TypeError: can only concatenate str (not "list") to str – FastAndCurious Oct 01 '20 at 07:45
2

When you write f.write("Page content:\n", content, "\n"), Python thinks you passed 3 arguments to the write method, because there are three elements separated by commas. write just needs the string you want to write to the file. In Python, string concatenation is made with + operator.

Also I suggest you to use the with statement before opening the file. It will make sure the file will be closed when you get out of the scope.

with open("output_from_wikibot.txt", "w", encoding="utf-8") as f:
    f.write("Page content:\n" + content + "\n")
    f.write("Page title:" + title + "\n")
    f.write("Categories:" + categories + "\n")
    f.write("Links:" + links + "\n")
    f.write("References:" + references + "\n")
    f.write("Summary:" + summary + "\n")
-1

The problem is exactly as the error message states. The API f.write() takes only one argument, it takes a single string, and writes it to a line in the file.

You will need to do string concatenation.

f.write("Page content:\n" + content)
Google is your friend

WilderField
  • 631
  • 1
  • 9
  • 16