Pandas read_csv 不读取文件(while 循环)

我正在尝试阅读我存储在电脑上的几张(~30)csv 表。


i=2

Liste1 = []

Liste2 = []

x = 0

while i < 32:

    string = str(i)

    if i < 10:

        try:


            Name = 'D:\\FTPDaten\\2020\\Alle\\2020010'+string+'.csv'

            Tabelle = pd.read_csv(Name, sep=';', decimal=",", header=0, usecols=[7, 20])

            Tabelle.columns = ['AC', 'DC']


            neueTabelle1 = Tabelle['AC']

            neueTabelle = Tabelle['DC']

            Schleifenlaenge = len(neueTabelle)

            j = 0

            del(Tabelle)

            while j < Schleifenlaenge:


                Datenwert1 = neueTabelle.iloc[j]

                Datenwert2 = neueTabelle1.iloc[j]

                Liste1.append(Datenwert1)

                Liste2.append(Datenwert2)

                j = j + 1

        except FileNotFoundError:

            i=i+1



    elif i >= 10 and i < 32:

        try:

            Name = 'D:\\FTPDaten\\2020\\Alle\\202001' + string + '.csv'

            Tabelle = pd.read_csv(Name, sep=';', decimal=",", header=0, usecols=[7, 20])

            Tabelle.columns = ['AC', 'DC']


            neueTabelle1 = Tabelle['AC']

            neueTabelle = Tabelle['DC']

            Schleifenlaenge = len(neueTabelle)

            j = 0

            

            while j < Schleifenlaenge:

                Datenwert1 = neueTabelle1.iloc[j]

                Datenwert2 = neueTabelle.iloc[j]

                Liste1.append(Datenwert1)

                Liste2.append(Datenwert2)

                j = j + 1

        except FileNotFoundError:

            i = i+1

    i = i + 1


当 while 循环第一次运行时,一切正常。当进入第一次迭代时,read_csv 不会像以前那样读取文件。我希望得到一个存储第 7 列和第 20 列的 DataFrame。但我确实得到了一个完全没有内容的 DataFrame - 只有标题。我尝试了很多,但肯定无法修复它。


慕无忌1623718
浏览 162回答 3
3回答

紫衣仙女

问题在于它如何读取';'其他文件(第一个文件除外)。如果你用 Excel 打开它们,你也许就能明白我在说什么。因此,您需要做的是跳过文件开头的那些行。import pandas as pdListe1 = []Liste2 = []for i in range(2,32):&nbsp; &nbsp; skipRows = 7&nbsp; &nbsp; if i != 2:&nbsp; &nbsp; &nbsp; &nbsp; skipRows += 1&nbsp; &nbsp; if i < 10:&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = 'D:\\FTPDaten\\2020\\Alle\\2020010{string}.csv'.format(string=i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Tabelle = pd.read_csv(Name, sep=';', decimal=",", header=0, usecols=[7, 20], skiprows=skipRows)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Tabelle.columns = ['AC', 'DC']&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i < 10:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Datenwert1 = list(Tabelle['DC'])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Datenwert2 = list(Tabelle['AC'])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif i >= 10 and i < 32:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Datenwert1 = list(Tabelle['AC'])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Datenwert2 = list(Tabelle['DC'])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Liste1 += Datenwert1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Liste2 += Datenwert2&nbsp; &nbsp; &nbsp; &nbsp; except FileNotFoundError as e:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(e)df = pd.DataFrame({'col1':Datenwert1,&nbsp; &nbsp;#<-- change 'col1', 'col2' to whatever you want to name them&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'col2':Datenwert2})

梦里花落0921

尝试在此处创建一个新的数据框,而不是迭代现有的数据框cols = ['AC', 'DC']new_Tabelle = pd.DataFrame(columns = cols)new_Tabelle['AC']=Tabelle['AC']new_Tabelle['DC']=Tabelle['DC']

largeQ

我没有 30 个分号分隔的文件。然而,这可以很容易地简化为仅拾取存在并匹配模式的文件,使用globimport pandas as pdfrom pathlib import Pathimport randomfor i in range(30):&nbsp; &nbsp; with open(f"2020010_os_{i}.csv", "w") as fp: fp.write(f"id;val\n{i};{random.randint(10,20)}\n")&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;pd.concat([pd.read_csv(fn, sep=";") for fn in Path().cwd().glob("2020010*.csv")])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python