python3将字节类转换为字符串

时间:2016-01-15 16:59:16

标签: python python-3.x encode

我使用

从Python 3中的套接字接收数据
data = ""
...
data = s.recv(1024)

当我想要print(data)print (type(data))时,它会告诉我:

b'contentofdata'
<class 'bytes'>

如何将data转换回字符串,以便contentofdata之后只显示print(data)?我使用.decode.encode尝试了很多,但它似乎在Python 3中不起作用。

1 个答案:

答案 0 :(得分:1)

您可以import UIKit import MapKit class ViewController: UIViewController, MKMapViewDelegate { @IBOutlet weak var mapView: MKMapView! override func viewDidLoad() { super.viewDidLoad() self.mapView.delegate = self let baseURL = NSBundle.mainBundle().bundleURL.absoluteString let urlTemplate = baseURL.stringByAppendingString("OSM_sat_tiles/{z}/{x}/{y}.png") //OSM_sat_tiles is the folder name which has the tiles. let layer = MKTileOverlay(URLTemplate: urlTemplate) layer.canReplaceMapContent = true self.mapView.addOverlay(layer) } func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer { if overlay is MKTileOverlay { let renderer = MKTileOverlayRenderer(overlay:overlay) renderer.alpha = 0.8 return renderer } return MKTileOverlayRenderer(overlay: overlay) } } 将字节转换为字符串。