简体   繁体   中英

Why do my text files return as empty when I import it in python even though there are values in the file?

I have tried to load a text file with 300000 integers in it. I load the text with the following code:

import numpy as np 
x = np.loadtxt("signal.txt", delimiter=",")
print(x)

And when I try to print the values, they return empty. How do I fix this problem? I've checked that the data set has values in it, and they are there.

This test case should run smoothly on your system. If that is true, you should have some problem with your original signals.txt file.... characters ... encoding ...

import numpy as np
import random

# generate file with 0s and 1s
with open('test.txt', 'w') as f:
    text = ', '.join([str(random.randint(0,1)) for _ in range(1000)])
    f.write(text)

array = np.loadtxt('test.txt', delimiter=',')
print(array.shape)
array

# to check signals.txt  ... print content
with open('signals.txt') as f:
    text = f.read()
    print(text)

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