Can Python remove double quotes from a string, when reading in text file?

隐身守侯 提交于 2019-11-30 06:38:53

The csv module (standard library) does it automatically, although the docs isn't very specific about skipinitialspace

>>> import csv

>>> with open(name, 'rb') as f:
...     for row in csv.reader(f, delimiter=' ', skipinitialspace=True):
...             print '|'.join(row)

5.6|4.5|6.8|6.5
5.4|8.3|1.2|9.3
for line in open(name, "r"):
    line = line.replace('"', '').strip()
    a, b, c, d = map(float, line.split())

This is kind of bare-bones, and will raise exceptions if (for example) there aren't four values on the line, etc.

There's a module you can use from the standard library called shlex:

>>> import shlex
>>> print shlex.split('5.6  4.5  6.8  "6.5"')
['5.6', '4.5', '6.8', '6.5']
for line in open(fname):
    line = line.split()
    line[-1] = line[-1].strip('"\n')
    floats = [float(i) for i in line]

another option is to use built-in module, that is intended for this task. namely csv:

>>> import csv
>>> for line in csv.reader(open(fname), delimiter=' '):
    print([float(i) for i in line])

[5.6, 4.5, 6.8, 6.5]
[5.6, 4.5, 6.8, 6.5]

Or you can simply replace your line

l = re.split("\s+",string.strip(line)).replace('\"','')

with this:

l = re.split('[\s"]+',string.strip(line))

I used in essence to remove the " in "25" using

Code:
        result = result.strip("\"") #remove double quotes characters 
A.R.B

I think the easiest and most efficient thing to do would be to slice it!

From your code:

d = l[3]
returns "6.5"

so you simply add another statement:

d = d[1:-1]

now it will return 6.5 without the leading and end double quotes.

viola! :)

You can use regexp, try something like this

import re
re.findall("[0-9.]+", file(name).read())

This will give you a list of all numbers in your file as strings without any quotes.

IMHO, the most universal doublequote stripper is this:

In [1]: s = '1 " 1 2" 0 a "3 4 5 " 6'
In [2]: [i[0].strip() for i in csv.reader(s, delimiter=' ') if i != ['', '']]
Out[2]: ['1', '1 2', '0', 'a', '3 4 5', '6']
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!