一个for循环中有多个命令,多个for循环中还有其他命令? [英] Multiple commands within a for-loop, multiple for-loops, or something else?

查看:139
本文介绍了一个for循环中有多个命令,多个for循环中还有其他命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一项任务(即没有大熊猫或其他任何使它更容易实现的任务),我必须在python3/numpy/Matplotlib中绘制一个多边形,但是数据文件给我带来了第一道麻烦,我无法确定找出导致问题的原因.

For a task (i.e. no pandas or anything that would make it easier), I have to plot a polygon in python3/numpy/Matplotlib, but the data file is giving me trouble at the first hurdle and I can't figure out what is causing problems.

.dat文件有两列,每行几行,如下所示:

The .dat file has 2 columns of several lines, each line looking like this:

(34235.453645, 68597.5469)

到目前为止,我已经弄清楚了如何使用.replace删除每行中的括号和逗号,因此可以:

So far I have figured out how to remove the brackets and the comma in every line with .replace so it makes:

34235.453645 68597.5469

与此:

lang-js
#Assign columns
data_easting=[]
data_northing=[]
    
#Open the .dat file (in Python)
Poly = open('poly.dat','r')
    
#Loop over each line of poly.dat.
for replace in Poly.readlines():
    #Replace the extraneous information ("(",",",")") with nothing, rather than multiple commands to strip and remove them. Stack Overflow Source for idea: https://stackoverflow.com/questions/390054/python-strip-multiple-characters.
    replace = replace.replace('(','').replace(',','').replace(')','')

#Loop over the replaced data to split into lines 1(easting) and 2(northing) and append.

但是,当我想拆分返回的替换"时,在最终注释后,将字符串分为x和y(data_northing和_easting列表),并在其中包含另一个for循环:

However when I want to split the returned "replace" string into x and y (the data_northing and _easting lists) after the final comment with another for-loop within:

        for line in replace.readlines():
                x,y=line.split()
                data_easting.append(x)
                data_northing.append(y)

我收到一条错误消息,说str替换"了字符串.不能使用readlines功能.

I get an error saying that the str "replace" can't use the readlines function.

如果我删除缩进(两个for循环都在同一级别),则会出现相同的错误.

If I remove the indentation (both for-loops at the same level), the same error appears.

如果我替换替换",带有"Poly"从先前的for循环中,列表"只是x,y数据的第一行(打印(x [n])只是返回数字字符串的第n个字符)

If I replace "replace" with "Poly" from the previous for-loop, the "list" is only the first line of x,y data (print (x[n]) just returns the nth character of the number string)

如果我像这样将循环和命令结合在一起;

If I combine the loops and commands together like so;

for replace, line in Poly.readlines():
        x,y =line.split
        data_easting.append(x)
        data_northing.append(y)

并尝试执行命令,我得到未定义行"的错误,或者我得到值错误: " ValueError:没有足够的值可以解压缩(预期2,得到1)"

and try the command I get an error that "Line is not defined", or I get a value error: "ValueError: not enough values to unpack (expected 2, got 1)"

到现在为止可能已经很明显了,我对Python还是很陌生,无法弄清楚如何克服这一点,继续进行数据绘制(我大部分都可以接受).我将如何使第二个功能跟从第一个功能相继,我是否要使第一个功能产生实际输出,然后运行第二个命令(即"Make Poly2.dat",然后拆分该数据文件)?

As it's probably evident by now, I'm very new to Python and can't figure out how to get past this to move on to plotting data (which I'm mostly ok with). How would I make the second function follow from the first, do I have make the first function make an actual output and then run second command (i.e. "Make Poly2.dat" and then split that datafile)?

当我搜索问题时,很多解决方案提出了迭代器.可以在这里应用吗?

When I've searched around for the problem, a lot of solutions bring up iterators. Could that be applied here?

我最终将其报废,并使用.strip('()\n'.split(', ')来获取数据,但是现在虽然我有2个仅包含所需数字的字符串列表,但仍然得到了Value Error; not enough values to unpack (expected 2, got 1). /p>

I ended up scrapping this and going for .strip('()\n' and .split(', ') to get the data, however now while I have 2 lists of strings with only the numbers I need, I still get a Value Error; not enough values to unpack (expected 2, got 1).

推荐答案

您似乎在朝所有错误的方向推送代码.您需要学习如何调试小型程序.

You seem to be pushing your code in all the wrong directions. You need to learn how to debug small programs.

通过这样的数据处理,您应该随手打印结果,然后您会看到每个阶段的数据.

With data processing like this you should print out the results as you go and you will see what the data looks like at each stage.

也许你是这个意思:

data_easting=[]
data_northing=[]
    
#Open the .dat file (in Python)
Poly = open('poly.dat','r')
    
#Loop over each line of poly.dat.
for replace in Poly.readlines():
    replace = replace.strip()
    if replace:
        replace = replace.replace('(','').replace(',','').replace(')','')
        print(replace)
        x,y = replace.split()
        data_easting.append(x)
        data_northing.append(y)

更新:使用strip()清理空白行.

这篇关于一个for循环中有多个命令,多个for循环中还有其他命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆