对多个csv文件执行操作

时间:2019-12-11 16:38:39

标签: python pandas csv

我有一个包含多个CSV文件的文件夹,我想对它们执行一些操作:

我需要阅读它们,然后删除每个文件的第一行,并从文件名中创建一个名为“ Date”的新列。 (文件名类似于campaign_by_date_2019_12_10_to_2019_12_10)然后合并所有文件。

我设法加入了所有文件,但是我不知道何时应该执行这些操作。

extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
#combine all files in the list
combined_csv = pd.concat([pd.read_csv(f) for f in filenames ])

在此先致以问候和感谢,

安德烈斯

2 个答案:

答案 0 :(得分:4)

public Clip callAPI(Source src){
    URL url;
    Clip result = null;

    AudioInputStream sound = null;
    {
        try {
            url = new URL(" http://api.voicerss.org/?key=" + keyAPI + "&hl=" + src.getLang() + "&src=" + src.getSrc());

            sound = AudioSystem .getAudioInputStream(url); //here i have the audio
            Object sound2 = AudioSystem.getAudioInputStream(url);
            AudioFormat at = sound.getFormat();
            result = AudioSystem.getClip();
            result.open(sound);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();

        }catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    }

    return result;
}

 Clip t = null;
 AudioInputStream ais = null;
 private TextToSpeechService textToSpeechService;
 public Clip theFunction(@RequestParam String src, HttpServletRequest request){
    //if(src == request.getSession().getAttribute("input")){
    Source tmp = new Source();
    tmp.setSrc(src);

    t = textToSpeechService.callAPI(tmp);


    t.start();

    return t;

}

请注意,切片.sidenav { height: 100%; width: 260px; position: fixed; z-index: 1; top: 140px; left: 135px; background: #eee; overflow-x: hidden; padding: 8px 0; border-radius: 5px; } .sidenav a { padding: 6px 8px 6px 16px; text-decoration: none; font-size: 22px; color: #2b8bc6; display: block; } .sidenav h3 { padding: 6px 8px 6px 16px; text-decoration: none; font-size: 22px; color: black; display: block; } .box22 { background-color: #fff; max-width: 1000px; margin: auto; margin-top: 20px; border-radius: 8px; }时,我假设所有文件名都以<body> <div class="sidenav"> <a href="#clients">HTML 1 editors</a> <a href="#clients">HTML 2 editors</a> <a href="#contact">HTML 3 editors</a> </div> <div class="box22"> <h1>HTML part 1 intro</h1> <p>text</p> </div> </body>结尾

答案 1 :(得分:2)

如果您以常规的for循环阅读csvs,而不是使用列表理解功能,将更容易创建日期列。

如果您不确定如何使用datetime来完成所需的操作,lmk和我会为此添加详细信息。

combined_df = pd.DataFrame()
for filename in filenames:
    # read csv
    df = pd.read_csv(filename)
    # delete first row
    df = df.iloc[1:]
    # create date column
    df['Date'] = datetime.datetime(filename, "your_date_format")
    # combine with others
    combined_df = combined_df.append(df, sort=False)