.tsv file is Tab Separated Value file. Each row of data is stored by using Tab space as delimiter. Each row ends with line break.
import pandas as pd
my_data = pd.read_table('D:\\student.tsv')
print(my_data)
Output is here ( there are more rocords , only 5 are displayed here )
id name class1 mark sex
0 1 John Deo Four 75 female
1 2 Max Ruin Three 85 male
2 3 Arnold Three 55 male
3 4 Krish Star Four 60 female
4 5 John Mike Four 60 female
--------------------------
--------------
The method read_table() is deprecated, use read_csv instead, passing sep='\t'.
import pandas as pd
my_data = pd.read_csv('D:\\student.csv',sep='\t')
print(my_data)
Example 1: Reading a File with a Custom Delimiter
import pandas as pd
df = pd.read_table("data.txt", sep=";")
print(df)
This example demonstrates how to read a file where fields are separated by a semicolon (;) instead of the default tab character.
By specifying the sep parameter, we can correctly parse files with custom delimiters.
Example 2: Skipping Specific Rows While Reading
import pandas as pd
df = pd.read_table("data.txt", skiprows=[0, 2])
print(df)
This code skips the first and third rows (indices 0 and 2) of the file while reading.
Useful when certain rows contain metadata or irrelevant information.
Example 3: Reading Only Specific Columns
import pandas as pd
df = pd.read_table("data.txt", usecols=["Name", "Age"])
print(df)
This example reads only the "Name" and "Age" columns from the file.
Efficient for large datasets where only specific columns are needed.
Example 4: Handling Missing Values
import pandas as pd
df = pd.read_table("data.txt", na_values=["NA", "?"])
print(df)
Specifies additional strings to recognize as NaN (missing values), such as "NA" and "?".
Ensures accurate representation of missing data in the DataFrame.
Example 5: Parsing Dates While Reading
import pandas as pd
df = pd.read_table("data.txt", parse_dates=["Date"])
print(df.dtypes)
Automatically parses the "Date" column into datetime objects.
Facilitates time series analysis and date-based operations.
Example 6: Setting a Specific Column as Index
import pandas as pd
df = pd.read_table("data.txt", index_col="ID")
print(df.head())
Sets the "ID" column as the index of the DataFrame.
Useful for data alignment and retrieval based on unique identifiers.
Example 7: Reading a File with Encoding Specification
import pandas as pd
df = pd.read_table("data.txt", encoding="utf-8")
print(df)
Specifies the encoding of the file, which is essential when dealing with non-ASCII characters.
Prevents encoding-related errors during file reading.
Example 8: Handling Bad Lines in the File
import pandas as pd
df = pd.read_table("data.txt", on_bad_lines="skip")
print(df)
Skips lines with too many fields (bad lines) instead of raising an error.
Ensures that the reading process continues smoothly despite malformed lines.
Example 9: Reading a Large File in Chunks
import pandas as pd
chunk_iter = pd.read_table("large_data.txt", chunksize=1000)
for chunk in chunk_iter:
print(chunk.head())
Reads the file in chunks of 1000 rows, which is memory-efficient for large files.
Allows processing of large datasets that cannot fit into memory entirely.