0

I'm switching from matlab to python. The data I want to import is like this

4
6
2
1
2.0E8
0.2
0.002

1 2 6
2 3 4
2 4 5
2 5 6

0 0
1 0
2 0
2 1
1 1
0 1

4 0 -150

1 1 1
6 1 1

And this is how I read it in matlab

FP1=fopen('471220580.txt','rt');
NELEM=fscanf(FP1,'%d',1);
NPION=fscanf(FP1,'%d',1);
NVFIX=fscanf(FP1,'%d',1);
NFORCE=fscanf(FP1,'%d',1);
YOUNG=fscanf(FP1,'%e',1);
POISS=fscanf(FP1,'%f',1);
THICK =fscanf(FP1,'%f',1);
LNODS=fscanf(FP1,'%d',[3, NELEM])';  
COORD=fscanf(FP1,'%f',[2,NPION])';  
FORCE=fscanf(FP1,'%f',[3,NFORCE])';  
FIXED=fscanf(FP1,'%d',[3,NVFIX])';  

How can I import these data in python? I didn't find an equivalent for fscanf in python.

For What is the equivalent of Matlab 'fscanf' in Python? numpy.loadtxt requires Each row in the text file must have the same number of values. This is not a good fit in my case.

4
  • You haven't responded on your last question still... might want to slow down with the questions a bit? Commented Apr 12, 2015 at 14:31
  • possible duplicate of What is the equivalent of Matlab 'fscanf' in Python? Commented Apr 12, 2015 at 14:32
  • @DonkeyKong, I've checked that answer, numpy.loadtxt requires Each row in the text file must have the same number of values.. Commented Apr 12, 2015 at 14:49
  • @DonkeyKong, as for my previous question, it seems that people think is not good to create matrix directly in script, so I create this post instead. The previous one is deleted. Commented Apr 12, 2015 at 14:53

2 Answers 2

0

Loop across the lines in the file like

for line in open("file.txt"):
     parse(line)

The parse method is a method which takes the line as input and output an integer, float or a list of ints.

For integer -> var = int(line)

For float -> var = float(line)

For list of ints -> var = map(int, line.split())

For a matrix -> consider that you read want a m X n matrix. So read in m lines, from above loop like -

matrix = []

count = m
for line in open():
    if count > 0:
        matrix.append(map(int, line.split()))
Sign up to request clarification or add additional context in comments.

1 Comment

But how can I read it as a matrix? LNODS=fscanf(FP1,'%d',[3, NELEM]); This would generate a matrix.
0
with open('471220580.txt') as text_file:
    all_numbers = text_file.read().split("\n")
    all_numbers = filter(None, all_numbers)
    #the all_numbers list contains all the distinct values from the file
    #Now to initialize various variables you can use following methods:

    #Method1:
    NELEM = all_numbers[0]
    NPION = all_numbers[1]
    #and so on ... 

    #Note: Keep in mind that the values stored in these variables is of type str


   #Method2:
   NELEM, NIPON ,... = all_numbers

The advantage of using Method1 over Method2 is that you can use type casting in method1 , and you can achieve this is by using var1 = type(var)

NELEM = int(all_numbers[0])
NPION = int(all_numbers[1])
POISS = float(all_numbers[5])

For the lines 9-12 which constitute a matrix we can represent matrix as list of lists.

matrix_row1 = map(int, all_numbers[9])    #[1, 2, 3]
matrix_row2 = map(int, all_numbers[10])   #[4, 5, 6]
matrix_row3 = map(int, all_numbers[11])   #[7, 8, 9]

matrix = [matrix_row1, matrix_row2, matrix_row3]
#[[1, 2, 3],
# [4, 5, 6],
# [7, 8, 9]]

3 Comments

But How can I locate the matrix? I need to manually count the numbers?
which matrix are you talking about ?
For example, from line 9-12 is a matrix. But it seems like we turned all numbers into an array? How can I import that into a matrix conviniently?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.