Checking a filename before reading it with pd.read_csvAsk Questions

 Posted on 11/06/2019

Hi Guys,

I'm trying to read a csv file according to its type (X or Y, so if the file name starts with 125 I read the raw_file1 file otherwise raw_file2) but when I run the code I get no ouput.

import string
import pandas as pd
import numpy as np
raw_file1 = '2340595954_header.csv' #csv typ X
raw_file2 = '4325670000_things.csv' #csv typ Y
# Get the first 3 digit/character of the raw_file1
first_3_char_raw_file1  = ''.join([s[0:3] for s in raw_file1.split(' ')]) #234
# Check if the first 3 digits/characters of the raw_file1 are 234(csv-type X) or 432(csv-type Y) 
#if yes then read raw_file1
if first_3_char_raw_file1 == 125:
    data = pd.read_csv(raw_file1)
#else read raw_file2
    data = pd.read_csv(raw_file2)

Share On: facebook gplus twitter
Asked by Navin Gondoliya on 11/06/2019 Score: 24 points
Add Comment:


1 Answers

0 Corrected Answers
Aproved Answers
Answered by Doug Stevenson on 11/7/2019 6:57:54 AM Score: 56 points

There is str.startswith method. So, you can just do:

Python Code:

if raw_file1.startswith('125'):
    pass # or do something
    pass  # or do something


Add Comment:

Post Your Answers

Existing Members

Sign in to your account
Email Address
New Member?
Sign up and complete profile
Full Name
Email Address
I have read and agree to the Terms of Service and Privacy Policy
Please subscribe me to the StoodQ newsletters
Guideline to answer a question:

Useful tips to submit your answer
Please read below guidelines before you submit your answer for question.

  • Read and understand question for which you are submitting your answer.
  • Try to avoid grammatical and spell mistake while answering.
  • Do not post any irrelevant information in your answer.
  • Explain your answer with example or any reference link to help who posted question.
  • If you find irrelevant question, please report it to support. Click here to contact support.
  • You agree to the privacy policy and terms of use to submit any contents.

Note: StoodQ is online developers community which helps developer for their difficulty, lets help them with your value contribution.