从.csv文件中获取行数

时间:2017-01-16 14:27:16

标签: python csv numpy

我正在编写一个Python模块,其中我读取了一个包含2列和随机行数的.csv文件。然后我通过这些行直到第1列> X。此时我需要当前行和上一行的数据进行一些计算。

目前,我正在使用'for i in range(rows)'但每个csv文件将有不同的行数,所以这不会起作用。

代码如下:

rows = 73
    for i in range(rows):  

        c_level = Strapping_Table[Tank_Number][i,0]  # Current level
        c_volume = Strapping_Table[Tank_Number][i,1] # Current volume

        if c_level > level:

            p_level = Strapping_Table[Tank_Number][i-1,0]    # Previous level
            p_volume = Strapping_Table[Tank_Number][i-1,1]  # Previous volume

            x = level - p_level   # Intermediate values
            if x < 0:
                x = 0
            y = c_level - p_level
            z = c_volume - p_volume

            volume = p_volume + ((x / y) * z)

            return volume

在玩数组时,我使用了:

for row in Tank_data: 
   print row[c]       # print column c
   time.sleep(1) 

这会遍历所有行,但我无法使用此方法访问以前的行数据。

我考虑过在每个循环中存储前一行和当前行,但在我这样做之前,我想知道是否有一种简单的方法来获取csv中的行数。

3 个答案:

答案 0 :(得分:1)

存储上一行

@Test
public void testOverlapOnce() throws Exception {
    assertEquals("", overlapOnce("", ""));
    assertEquals("ab", overlapOnce("a", "b"));
    assertEquals("abc", overlapOnce("ab", "bc"));
    assertEquals("abcdefghqabcdefghi", overlapOnce("abcdefgh", "efghqabcdefghi"));
    assertEquals("aaaaaabaaaaaa", overlapOnce("aaaaaab", "baaaaaa"));
    assertEquals("ccc", overlapOnce("ccc", "ccc"));
    assertEquals("abcabc", overlapOnce("abcabc", "abcabc"));

    /**
     *  "a" + "b" + "c" => "abc"
     "abcde" + "defgh" + "ghlmn" => "abcdefghlmn"
     "abcdede" + "dedefgh" + "" => "abcdedefgh"
     "abcde" + "d" + "ghlmn" => "abcdedghlmn"
     "abcdef" + "" + "defghl" => "abcdefghl"
     */
    assertEquals("abc", overlapOnce("a", "b", "c"));
    assertEquals("abcdefghlmn", overlapOnce("abcde", "defgh", "ghlmn"));
    assertEquals("abcdedefgh", overlapOnce("abcdede", "dedefgh"));
    assertEquals("abcdedghlmn", overlapOnce("abcde", "d", "ghlmn"));
    assertEquals("abcdefghl", overlapOnce("abcdef", "", "defghl"));


    // Consider str1=abXabXabXac and str2=XabXac. Your approach will output abXabXabXacXabXac because by
    // resetting j=0, it goes to far back.
    assertEquals("abXabXabXac", overlapOnce("abXabXabXac", "XabXac"));

    // Try to trick algo with an earlier false match overlapping with the real match
    //  - match first "aba" and miss that the last "a" is the start of the
    // real match
    assertEquals("ababa--", overlapOnce("ababa", "aba--"));
}

或者你可以将它与发电机一起使用

with open("myfile.txt", "r") as file:
     previous_line = next(file)
     for line in file:
        print(previous_line, line)
        previous_line = line

答案 1 :(得分:0)

您应该使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <header class="mobile-menu">Text here</header> <section>Sugar plum muffin cookie pastry oat cake icing candy canes chocolate. Gummi bears chupa chups fruitcake dessert jelly. Muffin cookie ice cream soufflé pastry lollipop gingerbread sweet. Unerdwear.com bonbon candy marzipan bonbon gummies chocolate cake gummi bears powder. Unerdwear.com tart halvah chocolate cake dragée liquorice. Sugar plum chocolate bar pastry liquorice dragée jelly powder. Jelly tootsie roll applicake caramels. Marzipan candy tootsie roll donut. Gummies ice cream macaroon applicake.</section>

enumerate

答案 2 :(得分:0)

由于csv中每行的大小在读取之前是未知的,因此如果要查找行数,则必须进行初始传递,例如:

numberOfRows = (1 for row in file)

然而,这意味着你的代码将会读取csv两次,如果它非常大你可能不想这样做 - 将每一次迭代存储到全局变量的简单选项可能是最好的选择在那种情况下。

另一种途径可能是只读取文件并从中进行分析。熊猫DataFrame(http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html) 但如果你的csv太大,这可能会导致缓慢。