简体   繁体   English

Python发布输出。 字符串操作

[英]Python issues output. String manipulation

I got the following code, however at the time of the output (txt file) it will not return a value just a None however that happensw when I save it to a txt file, when I simply run the program it does display the values that I want. 我得到以下代码,但是在输出(txt文件)时,它不会仅返回None值,但是当我将其保存到txt文件时会发生这种情况,当我简单地运行程序时,它确实显示了我想要。

archi2=open('googletabla.txt', 'w')

nicCoor=open("C:\Users\SistBa\PycharmProjects\seguropais\SeguroPais\NIC.txt", "r")
coorNiclines=nicCoor.readlines()

poly = []

for NicaLines0 in coorNiclines: #leyendo las coordenadas de Nicaragua
    if(len(NicaLines0))>5:
        NicLines=re.sub(r'99.000', '',str(NicaLines0))
        NicLines1=re.sub(r' 0.000', '',str(NicLines))
        if NicLines1.isspace()==False :
            line = ','.join(NicLines1.split())
            lista=str(poly.extend(zip(*[iter(map(float, line.split(',')))]*2)))

            archi2.write(lista+'\n')
print poly
archi2.close()

My output on python is: 我在python上的输出是:

C:\Python27\python.exe C:/Users/SistBa/PycharmProjects/seguropais/SeguroPais/game
[(14.993, -83.152), (14.959, -83.172), (14.952, -83.2), (14.891, -83.262), (14.762, -83.31), (14.633, -83.283), (14.456, -83.221), (14.333, -83.2), (14.265, -83.214), (14.122, -83.297)...]

Process finished with exit code 0 流程结束,退出代码为0

however on my txt file is the following: 但是在我的txt文件上是以下内容:

None
None
None
None
None
None
None
None...

Visual addition to answer above. 视觉上回答以上。

The problem here is that extend actually always returns None 这里的问题是,扩展实际上总是返回None

>>> a.extend('a')
>>> a
['a']

So change it to something like this: 因此,将其更改为如下所示:

archi2=open('googletabla.txt', 'w')

nicCoor=open("C:\Users\SistBa\PycharmProjects\seguropais\SeguroPais\NIC.txt", "r")
coorNiclines=nicCoor.readlines()

poly = []

for NicaLines0 in coorNiclines: #leyendo las coordenadas de Nicaragua
    if(len(NicaLines0))>5:
        NicLines=re.sub(r'99.000', '',str(NicaLines0))
        NicLines1=re.sub(r' 0.000', '',str(NicLines))
        if NicLines1.isspace()==False :
            line = ','.join(NicLines1.split())
            new_val = zip(*[iter(map(float, line.split(',')))]*2)
            poly.extend(new_val)
            lista=str(new_val)

            archi2.write(lista+'\n')
print poly
archi2.close()

You are string-converting the result of the list-method "extend", and write this. 您正在对列表方法“ extend”的结果进行字符串转换,并将其写入。 But it returns None, so that's why you end up with a bunch of None in your output. 但是它返回None,所以这就是为什么在输出中得到一堆None的原因。

Also, you probably don't even want the extend, because you write in each iteration, so for all NicaLines0 you will write it and all the preceding lines. 另外,您可能甚至不需要扩展,因为您是在每次迭代中编写的,因此对于所有NicaLines0,您都将编写它以及所有前面的行。

Then you completely unnecessarily split the NicLines1 only to join it again and then split it. 然后,您完全不必要地将NicLines1拆分为仅再次加入它,然后拆分它。 Just write 写吧

line = NicLines1.split()

and remove the line.split(",") on the following line. 并删除下一行的line.split(“,”)。

You will at some point getting into troubles using backslashes in filenames if you don't either properly escape them, or use raw-strings. 如果您没有正确转义文件名或使用原始字符串,则有时会在文件名中使用反斜杠而遇到麻烦。

And I don't know what your desired output is, so I can't suggest a replacement for the lista-assignment. 而且我不知道您想要的输出是什么,因此我不能建议替代lista分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM