【问题标题】:How to read a folder with .txt_ files in Jupiter notebook如何在 Jupyter 笔记本中读取包含 .txt 文件的文件夹
【发布时间】:2020-03-31 20:14:57
【问题描述】:

嘿,我有一个文件夹,其中包含 1985 - 2018 年期间的 116 个 .txt_ 文件,其中包含径流值。 如何一次打印所有电台? 我有一个代码,但一次只打印一个 txt 文件。 我的代码:

from pandas import read_csv

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import os

import cartopy

from datetime import date,datetime

dir1 = "minestasjoner/" #the name of the folder at my desktop 

files = os.listdir(dir1)

files = np.sort(files)

files_txt = [i for i in files if i.endswith('.txt_')]

df = pd.read_csv(dir1+files_txt[0],skiprows=6,header=None, index_col=0,sep=" 
",na_values=-9999, parse_dates = True)

df.index = pd.to_datetime(df.index,format="%Y%m%d/%H%M")

【问题讨论】:

  • 这仍然和您其他 3 个最新问题中的问题相同吗?

标签: python jupyter-notebook


【解决方案1】:

因为你的代码只读取一个文件

files_txt = [i for i in files if i.endswith('.txt_')]
tmp = []

for file_name in files_txt:
   tmp.append(pd.read_csv(dir1+file_name,skiprows=6,header=None, index_col=0,sep=" 
",na_values=-9999, parse_dates = True))

df = pd.concat(tmp)
df.index = pd.to_datetime(df.index,format="%Y%m%d/%H%M")

【讨论】:

  • 但是当我这样做时,它不会打印任何东西.. ?
  • @madelen 你是什么意思?这些代码中没有打印。
  • 我的意思是如何绘制所有的 txt 文件?
  • @madelen 我还是不明白。该脚本会将所有文本文件聚合到 pandas 数据框。如果您只想在屏幕上打印出来。你可以打印数据框
  • 我明白了。但是你能帮我做点什么吗?我可以发布图片,因为代码很长。我需要帮助来绘制水流时间序列与 SPI 1、SPI 3 和 SPI 6 的时间序列之间的相关性
猜你喜欢
  • 2021-05-28
  • 2021-11-18
  • 1970-01-01
  • 2020-05-08
  • 1970-01-01
  • 2018-11-09
  • 1970-01-01
  • 1970-01-01
  • 2016-07-24
相关资源
最近更新 更多