Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: 'charmap' codec can't encode characters #16

Open
ShantanuPathak opened this issue Jun 6, 2020 · 3 comments
Open

Error: 'charmap' codec can't encode characters #16

ShantanuPathak opened this issue Jun 6, 2020 · 3 comments

Comments

@ShantanuPathak
Copy link

Complete noob here, so excuse my naivete

I was getting the error "'charmap' codec can't encode characters ", I googled the error and changed the code

if args.usage == "WT":
        with open('output.txt', 'w') as file:
            for post in postBigDict:
                file.write(json.dumps(post))  # use json load to recover

    elif args.usage == "CSV":
        with open('data.csv', 'w',) as csvfile:
           writer = csv.writer(csvfile)
           #writer.writerow(['Post', 'Link', 'Image', 'Comments', 'Reaction'])
           writer.writerow(['Post', 'Link', 'Image', 'Comments', 'Shares'])

           for post in postBigDict:
              writer.writerow([post['Post'], post['Link'],post['Image'], post['Comments'], post['Shares']])
              #writer.writerow([post['Post'], post['Link'],post['Image'], post['Comments'], post['Reaction']])

    else:
        for post in postBigDict:
            print("\n") 

to this
`

 if args.usage == "WT":
        with io.open('output.txt', 'w', encoding='utf-8') as file:
            for post in postBigDict:
                file.write(json.dumps(post))  # use json load to recover

    elif args.usage == "CSV":
        with io.open('data.csv', 'w', encoding='utf-8') as csvfile:
           writer = csv.writer(csvfile)
           #writer.writerow(['Post', 'Link', 'Image', 'Comments', 'Reaction'])
           writer.writerow(['Post', 'Link', 'Image', 'Comments', 'Shares'])

           for post in postBigDict:
              writer.writerow([post['Post'], post['Link'],post['Image'], post['Comments'], post['Shares']])
              #writer.writerow([post['Post'], post['Link'],post['Image'], post['Comments'], post['Reaction']])

    else:
        for post in postBigDict:
            print("\n")

`

It then worked.

@faizananwerali
Copy link

+1
Add import io on top.
Also create pull request so it gets updated for all users

@ShantanuPathak
Copy link
Author

Also create pull request so it gets updated for all users

Sorry, I am not much of a coder so do not know what a pull request is

@visheshaylani
Copy link

encoding='utf-8' is built-in in the python 3. So, no need to add io.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants