将输出格式化为特定的json

时间:2019-03-29 11:01:06

标签: python json python-3.x

我想将JSON格式化为特定值。我的最终目标是转化 -33.494 143.2104{"lat":-33.494,"lon":143.2104}

我正在将IP转换为地理位置的纬度和经度:

python3.5 -c "
import geoip2.database; reader=geoip2.database.Reader('/geoip_databases/GeoLite2-City.mmdb'); 
response = reader.city('1.2.3.4');
print (response.location.latitude, response.location.longitude)"

-33.494 143.2104

试图将其转换为所需的输出:

python3.5 -c "
import geoip2.database; reader=geoip2.database.Reader('/geoip_databases/GeoLite2-City.mmdb'); 
response = reader.city('1.2.3.4')
;a=(response.location.latitude, response.location.longitude); 
b=('{"lat":' + a + '}');
c=b.replace(' ',',"lon"'); 
print (c)"

Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: Can't convert 'tuple' object to str implicitly

如何完成下面的输出{“ lat”:-33.494,“ lon”:143.2104}仍然只有一个衬里?

1 个答案:

答案 0 :(得分:0)

您可以尝试制作import React, { Component } from 'react'; import { AppRegistry, Button, StyleSheet, NativeModules, Platform, Text, View } from 'react-native'; import RNPrint from 'react-native-print'; export default class RNPrintExample extends Component { state = { selectedPrinter: null } // @NOTE iOS Only selectPrinter = async () => { const selectedPrinter = await RNPrint.selectPrinter() this.setState({ selectedPrinter }) } // @NOTE iOS Only silentPrint = async () => { if (!this.state.selectedPrinter) { alert('Must Select Printer First') } const jobName = await RNPrint.print({ printerURL: this.state.selectedPrinter.url, html: '<h1>Silent Print</h1>' }) } //trying to style this HTML async printHTML() { await RNPrint.print({ html: '<h1>Heading 1</h1><h2>Heading 2</h2><h3>Heading 3</h3>' }) } async printRemotePDF() { await RNPrint.print({ filePath: 'https://graduateland.com/api/v2/users/jesper/cv' }) } customOptions = () => { return ( <View> {this.state.selectedPrinter && <View> <Text>{`Selected Printer Name: ${this.state.selectedPrinter.name}`}</Text> <Text>{`Selected Printer URI: ${this.state.selectedPrinter.url}`}</Text> </View> } <Button onPress={this.selectPrinter} title="Select Printer" /> <Button onPress={this.silentPrint} title="Silent Print" /> </View> ) } render() { return ( <View style={styles.container}> {Platform.OS === 'ios' && this.customOptions()} <Button onPress={this.printHTML} title="Print HTML" /> <Button onPress={this.printPDF} title="Print PDF" /> <Button onPress={this.printRemotePDF} title="Print Remote PDF" /> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, }); ,然后再尝试制作json.dumps

dict