回答編集履歴

1

追記

2019/04/24 04:32

投稿

quickquip
quickquip

スコア11038

test CHANGED
@@ -107,3 +107,47 @@
107
107
  b'\ufeff\u3075'
108
108
 
109
109
  ```
110
+
111
+
112
+
113
+ ----
114
+
115
+ KSwordOfHaste さんの回答の続きのような形になりますが、
116
+
117
+
118
+
119
+ ```plain
120
+
121
+ % echo -n 'あ' | LANG=C python -c 'import sys; print(sys.stdin.read().encode("unicode_escape"))'
122
+
123
+ b'\udce3\udc81\udc82'
124
+
125
+ % LANG=C python -c 'import sys; print(sys.stdin.encoding, sys.stdout.encoding)'
126
+
127
+ US-ASCII US-ASCII
128
+
129
+ ```
130
+
131
+ `LANG=C` の設定下だと`sys.stdin`の方も US-ASCII encoding になる影響を受けるので、
132
+
133
+
134
+
135
+ ```python
136
+
137
+ sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding="utf-8")
138
+
139
+ sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
140
+
141
+ ```
142
+
143
+ とするのがいいようです。
144
+
145
+
146
+
147
+ ```plain
148
+
149
+ % echo -n 'あ' | LANG=C python -c 'import io, sys; sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding="utf-8"); print(sys.stdin.read().encode("unicode_escape"))'
150
+
151
+ b'\u3042'
152
+
153
+ ```