简体   繁体   English

我的Python代码没有从.csv excel文件中提取所有信息,而且我不知道为什么

[英]My Python code isn't pulling all the information from the .csv excel file and I cannot figure out why

I'm pretty new to the coding scene and my boss has kinda just thrown me into the fire, so I'm sorry if this is some rookie mistake, though I don't think it is. 我对编码领域还很陌生,而我的老板也把我惹上火了,所以如果这是菜鸟的错误,我很抱歉,尽管我认为不是。

So I'm trying to graph G versus l1 (that's not an eleven, but an L1). 因此,我尝试绘制G与l1的关系图(这不是11,而是L1)。 The data is in the file that I loaded from an excel file. 数据在我从excel文件加载的文件中。 The excel file is 14x250 so there are 14 arguments, each with 250 data points. excel文件为14x250,因此有14个参数,每个参数包含250个数据点。 The graph I put up is formatted correctly, but zero y-values show up. 我放置的图形格式正确,但是显示了零个y值。 From what I can tell, the code is graphing the first 12 columns and leaving the last 2 columns (which contain the two sets of 'y' data) out. 据我所知,该代码绘制了前12列的图形,而省略了后2列(其中包含两组“ y”数据)。 I cannot find where the error is and it's driving me crazy! 我找不到错误所在,这使我发疯! ANY help would be greatly appreciated! 任何帮助将不胜感激!

This is where I formatted the excel file and where I believe the mistake is: 这是我格式化excel文件的地方,我认为错误是:

header = ['l1', 'l2', 'l3', 'l4', 'l5', 'EI',
      'S', 'P_right', 'P1_0', 'P3_0',
      'w_left', 'w_right', 'G_left', 'G_right']

def loadfile(filename, skip=[], *args):
    output = []
    with open(filename, *args) as f:
        reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
        for i, row in enumerate(reader):
            if not(i in skip):
                output.append(row)
    return np.array(output)

This is where I actually loaded the excel file, which is the next likely place there is a mistake: 这是我实际加载excel文件的地方,这是下一个可能出现错误的地方:

outputs_l1 = [loadfile('FILE.csv'.format(p)) for p in p3_arr]

fig = plt.figure()
for output, col in zip(outputs_l1, colors):
    plt.plot(output[:,0], output[:,10]*1E3, col+'-')
plt.legend(['$P3 = {} Pa$'.format(p) for p in p3_arr], loc=(1.05, 0.6), fontsize=16)
for output, col in zip(outputs_l1, colors):
    plt.plot(output[:,0], output[:,11]*1E3, col+'--')
plt.ticklabel_format(axis='both', style='plain', scilimits=(-1,1))
plt.xlabel('$l1 (m)$')
plt.ylabel('G $(J / m^2) * 10^{-3}$')
plt.xlim(xmin=.2)
plt.ylim(ymax=2, ymin=0)

plt.subplots_adjust(top=0.8, bottom=0.15, right=0.7)

Using column names instead of numbers and combining the two plotting loops, 使用列名代替数字,并结合两个绘图循环,

col = {name:i for i,name in enumerate(header)}

fig = plt.figure()
for data,color in zip(outputs_l1, colors):
    xs  = data[:, col["l1"     ]]
    wl = data[:, col["w_left" ]] * 1000.0    # column 10
    wr = data[:, col["w_right"]] * 1000.0    # column 11
    plt.plot(xs, wl, color + "-", wr, color + "--")

a possible error becomes clear: you say you want to graph G , but you are specifying w instead (cols 10 and 11, instead of cols 12 and 13). 一个可能的错误变得很明显:您说要绘制G ,但您指定的是w (列10和11,而不是列12和13)。

Maybe w_left and w_data do not appear because they are outside your specified limits ( plt.ylim(ymax=2, ymin=0) )? 也许w_leftw_data不会出现,因为它们超出了您指定的限制( plt.ylim(ymax=2, ymin=0) )?

I think what you actually want is 我想你真正想要的是

fig = plt.figure()
for data,color in zip(outputs_l1, colors):
    xs  = data[:, col["l1"     ]]
    gl = data[:, col["G_left" ]] * 1000.0    # column 12
    gr = data[:, col["G_right"]] * 1000.0    # column 13
    plt.plot(xs, gl, color + "-", gr, color + "--")

In

def loadfile(filename, skip=[], *args):

passing [] as a default argument is a bit dangerous because changes to the default list can persist across calls to the function, causing very strange results. []作为默认参数传递会有些危险,因为对默认列表的更改会在函数调用期间持续存在,从而导致非常奇怪的结果。 You should instead do 你应该做

def loadfile(filename, skip=None, *args):
    skip = skip or []

except you are only using skip for membership-testing, so it would be faster as a set, so it becomes 除非您只使用skip进行成员资格测试,所以作为一个集合它会更快,所以它成为

def loadfile(filename, skip=None, *args):
    skip = set(skip or [])

Also, 也,

    output = []
    with open(filename, *args) as f:
        reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
        for i, row in enumerate(reader):
            if not(i in skip):
                output.append(row)
    return np.array(output)

could be shortened to 可以缩短为

    with open(filename, *args) as f:
        cr = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
        return np.array(row for i,row in enumerate(cr) if i not in skip)

and the result is a numpy 2d array; 结果是一个numpy 2d数组; so 所以

outputs_l1 = [loadfile('FILE.csv'.format(p)) for p in p3_arr]

looks like p3_arr is a list determining which data-sets you want to look at; 看起来p3_arr是确定要查看哪些数据集的列表; then outputs_l1 becomes a list of 2d arrays. 然后outputs_l1成为二维数组的列表。 This is a misleading name because, looking at header , l1 is supposed to be the first column of each 2d array; 这是一个误导性的名称,因为从header来看, l1应该是每个2d数组的第一列; thus outputs_l1 logically ought to be a 2d array containing just first-column data. 因此, outputs_l1逻辑上应该是仅包含第一列数据的2d数组。 I suggest renaming it to something like p_data . 我建议将其重命名为p_data

暂无
暂无

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

相关问题 我不明白为什么我的 python 代码没有以英里为单位返回答案 - I cant figure out why my python code isn't returning the answer in miles 遵循 python 学习教程,但我的代码没有显示正确的 output,我不知道为什么 - Following a python learning tutorial but my code isn't showing the correct output and I can't figure out why 我不知道为什么我的 web 抓取代码不起作用 - I can't figure out why my web scraping code isn't working 无法弄清楚为什么我的字母没有在Python的Caesar代码中旋转 - cannot figure out why my letters are not rotating in the Caesar code in Python Python-我的部分代码不起作用,我不知道为什么 - Python- Parts of my code aren't working and I can't figure out why 在我的代码中没有得到我需要的输出,我不知道为什么。 Python - Not getting the output I need in my code and I can't figure out why. Python 我不知道如何将数据写入到csv文件中以及如何正确执行排序代码 - I can't figure out how to write to my data to a csv file as well as having my sorting code function correctly 无法弄清楚为什么报表打印不正确 - Cannot figure out why statement isn't printing correctly Pygame 没有移动我的矩形,我不知道为什么? - Pygame isn't moving my rectangle and I can't figure out why? 我不知道为什么我的makemigrations无法在Django中工作,它显示以下错误 - I can't figure out why my makemigrations isn't working in django,its showing following error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM