Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python删除'';串串的?_Python_String_Text_Text Files - Fatal编程技术网

python删除'';串串的?

python删除'';串串的?,python,string,text,text-files,Python,String,Text,Text Files,我正在尝试从我的树莓皮B+上的GPS模块获取经度、纬度和高度 当前正在此处运行此代码: ## Prints the latitude and longitude every second. import time import microstacknode.hardware.gps.l80gps if __name__ == '__main__': gps = microstacknode.hardware.gps.l80gps.L80GPS() while True:

我正在尝试从我的树莓皮B+上的GPS模块获取经度、纬度和高度

当前正在此处运行此代码:

## Prints the latitude and longitude every second.
import time
import microstacknode.hardware.gps.l80gps

if __name__ == '__main__':
    gps = microstacknode.hardware.gps.l80gps.L80GPS()
    while True:
        try:
            data = gps.get_gpgga()
        except microstacknode.hardware.gps.l80gps.NMEAPacketNotFoundError:
            continue
        List = [list(data.values())[x] for x in [7, 9, 12]]
        string=str(List)
        string = string[1:-1]
        text_file = open("/home/pi/fyp/gps.txt","a")
        text_file.write(string + "\n")
        time.sleep(1)
这是上述代码的输出:

0.0, 0.0, '10.2'
0.0, 0.0, '3.2'
0.0, 0.0, '10.1'
0.0, 0.0, '3.1'
0.0, 0.0, '4.5'
0.0, 0.0, '20.1'
0.0, 0.0, '3583.1232'
0.0, 0.0, '102.01'
0.0, 0.0, '32.131'
0.0, 0.0, '421.32'
0.0, 0.0, '12391.11'
0.0, 0.0, '323.411'

是否可以删除输出最后一部分中的“”引号,只保留输出前两部分中的数字?

因为您要将列表转换为字符串,实际上不应该这样做。我建议改为使用:

让我们看看有什么问题:

>>> l = ['123', 456]
>>> str(l)
"['123', 456]"

>>> print(str(l))
['123', 456]

>>> ', '.join(map(str, l))
'123, 456'

>>> print(', '.join(map(str, l)))
123, 456
因为Python使用引号来表示:这个对象是一个字符串


当你转换一个包含string对象的对象时,引号将仍然存在,而不是自动删除

简而言之:
text\u file.write(string.replace(“,”)+“\n”)
。此外,您是否尝试用谷歌搜索问题的标题?
>>> l = ['123', 456]
>>> str(l)
"['123', 456]"

>>> print(str(l))
['123', 456]

>>> ', '.join(map(str, l))
'123, 456'

>>> print(', '.join(map(str, l)))
123, 456