I have a problem. I have a csv file which has no "," as delimiter but is built as a common excel file.
# 2016-01-01: Prices/Volumes for Market
23-24 24,57
22-23 30,1
21-22 29,52
20-21 33,07
19-20 35,34
18-19 37,41
I am only interested in reading in the second column for eg 24,57 in the first line. The data has no header. How could I proceed here?
pd.read_csv(f,usecols = [2])
Does not work because I think there is no column identified. Thanks for your help!
Try this:
pd.read_csv(f, delim_whitespace=True, names=['desired_col_name'], usecols=[1])
alternatively you might want to use pd.read_fwf
May be it is not suitable to read it as CSV
try to use regular expression, process it line by line
https://docs.python.org/2/library/re.html
for example
import re
>>> re.search('(\d{2})-(\d{2}) (\d{2}),(\d{2})', "23-24 24,57").group(1)
'23'
>>> re.search('(\d{2})-(\d{2}) (\d{2}),(\d{2})', "23-24 24,57").group(2)
'24'
>>> re.search('(\d{2})-(\d{2}) (\d{2}),(\d{2})', "23-24 24,57").group(3)
'24'
>>> re.search('(\d{2})-(\d{2}) (\d{2}),(\d{2})', "23-24 24,57").group(4)
'57'
To read file line by line in python, read this: How to read large file, line by line in python
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.