简体   繁体   中英

How to read multiple tables from one TXT file in pandas?

I have a text file that looks like this,

data = '''1|b|c 
2|e|f|g|h|i|j|k
2|2|3|4|5|6|7|8
1|e|f'''

I want to use pandas to create multiple tables from the data.

  1. Create table with rows starting with 1
  2. Create table with rows starting with 2

What is the recommended fast & easy way to get this done using pandas ?

You can just set delimiter on pandas read, as in:

# Or .read_table
master_table = pd.read_csv("file.txt", delimter="|")

# Select just the rows where an arbitrary column is 1.
df1 = master_table[master_table["column_name"] == 1].copy()

Perhaps it is simpler to iterate through the file:

with open("file.txt", "r") as file:
      for line in file:
           if line[0] == 1: # Check any arbitrary condition
               # Process the data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM