You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python情感分析:字符串输入异常及Google Cloud API编码错误求助

Hey there, let’s work through these two issues blocking your Python sentiment analysis project—no stress, we’ll get them sorted out in time!


问题1:无法输入字符串

First up, if you’re having trouble getting string input (whether from terminal interaction or file reads), here are two common fixes depending on your scenario:

情况A:交互式终端输入编码问题

If you’re using input() in Python 3 but hitting encoding errors when entering non-ASCII characters (like Chinese, emojis, etc.), your terminal’s default encoding might be misconfigured. Try these steps:

  1. Set the Python I/O encoding explicitly before taking input:
import os
os.environ['PYTHONIOENCODING'] = 'utf-8'

user_text = input("Enter your text here: ")
  1. Alternatively, read input directly from stdin with explicit encoding:
import sys
user_text = sys.stdin.read().encode('utf-8').decode('utf-8').strip()

情况B:从文件读取字符串失败

If the issue is reading strings from a file, don’t rely on the default encoding (which varies by OS)—specify the correct one upfront:

# Replace 'GBK' with your file's actual encoding (we'll show how to detect this next!)
with open("your_input_file.txt", "r", encoding="GBK", errors="replace") as f:
    file_content = f.read()

问题2:Google Cloud API UTF-8编码错误

That error message tells us the string you’re passing to the API isn’t valid UTF-8. Here’s how to fix it:

Step 1: Detect your file’s actual encoding

First, find out what encoding your file uses (it might be GBK, ISO-8859-1, etc.). Use the chardet library to check:

import chardet

with open("your_file_path", "rb") as f:
    raw_data = f.read()
    encoding_result = chardet.detect(raw_data)

print(f"Detected file encoding: {encoding_result['encoding']}")

Install chardet if you haven’t already: pip install chardet

Step 2: Read the file with the correct encoding and convert to UTF-8

Once you know the encoding, read the file properly, then ensure it’s converted to a UTF-8 compliant string before sending to Google Cloud:

from google.cloud import language_v1

def analyze_sentiment(text):
    client = language_v1.LanguageServiceClient()
    document = language_v1.Document(
        content=text,
        type_=language_v1.Document.Type.PLAIN_TEXT
    )
    return client.analyze_sentiment(request={"document": document})

# Use the encoding we detected earlier
file_encoding = encoding_result['encoding']  # e.g., 'GBK'
with open("your_file_path", "r", encoding=file_encoding, errors="replace") as f:
    # Read the content as a Unicode string
    raw_text = f.read()
    # Convert to UTF-8 (optional but safe for the API)
    utf8_text = raw_text.encode("utf-8").decode("utf-8")
    
    # Pass to Google Cloud API
    sentiment_result = analyze_sentiment(utf8_text)
    print(f"Sentiment score: {sentiment_result.document_sentiment.score}")
  • The errors="replace" parameter replaces any unreadable characters with � instead of crashing the script.
  • Google Cloud’s Language API requires UTF-8 encoded text, so converting explicitly ensures compatibility.

内容的提问来源于stack exchange,提问作者Thanatos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:50:25