python import csv file appears SyntaxError problem analysis

  • 2020-06-19 10:34:57
  • OfStack

background

np. loadtxt() is used to load data from text.

Every line in a text file must contain the same data.

***

loadtxt(fname,dtype=<class'float'>,comments='#',delimiter=None,converters=None,skiprows=0,usecols=None,unpack=False,ndmin=0)

fname The file, filename, or generator to read.

dtype Data type, default float.

comments The comments.

delimiter Delimiter, the default is a space.

skiprows Skip the first few lines of reading, default is 0, must be int integer.

usecols : Which columns to read, 0 is column 1. For example, usecols= (1,4,5) extracts columns 2,5, and 6. All columns are read by default.

unpack If it is True , reads the columns separately.

The problem

When I read files in ipython today,

The code is:


import numpy as np
x = np.loadtxt('C:\Users\sunshine\Desktop\scjym_3yNp3Gj\ The source data \000001.csv',delimiter= ',',skiprows=(1),usecols= (1,4,6),unpack= False)

The following error occurs:


SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

After searching the problem, the following solution was found:


r'C:\Users\expoperialed\Desktop\Python'
'C:\\Users\\expoperialed\\Desktop\\Python'
'C:/Users/expoperialed/Desktop/Python'

When I saw this, I knew where I was wrong.

There are a few things to note about writing strings:

1. Long strings

Very long strings that span multiple lines can be replaced by three quotes.

That is:


print('''This is a very long string.
it will continue.
and it's not over yet.
''hello,world''
still here.'''

Notice that you can use both single and double quotes in a string this way

2. The original string

namely print(r'c:\nwhere')

Backslashes have a special purpose, which is to escape and help you to include things like single and double quotes in strings that you can't put in directly.

\n, line break, can be stored in a string.

In the code block above, it's clear that we want a path, and if we don't use the original string, we'll get it

fname0

Yes, to prevent this, we can also escape with a backslash, but if the path is long, like the path in this article:

C:\\\Users\\\sunshine\\\Desktop\\\scjym_3yNp3Gj\\\源数据\\\000001.csv

If you use a double slash, you're going to have trouble.

In this case, we can use the original string.

The original string begins with r.

The original string cannot end with a backslash.

To end with a backslash, print(r'C:\Programfiles\foo\bar''\\') namely C:\Programfiles\foo\bar\

In the regular python string, \U character combinations represent extended Unicode code point escapes.

So there's an error here.

python Three ways to import csv files


# Primitive way 
lines = [line.split(',') for line in open('iris.csv')]
df = [[float(x) for x in line[:4]] for line in lines[1:]]
# use numpy package 
import numpy as np
lines = np.loadtxt('iris.csv',delimiter=',',dtype='str')
df = lines[1:,:4].astype('float')
# use pandas package 
import pandas as pd
df = pd.read_csv('iris.csv')
df=df.ix[:,:4]

Of the three methods, the last one is the simplest but takes a long time. The first one is the most troublesome but takes the least time. This can be done through the magic function in ipython %%timeit Come and see.

conclusion

Above is the analysis of SyntaxError problem in python import csv file, I hope it will be helpful to you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: