-2

When I use:

with open("test.txt") as file
    read = csv.reader(file)
    for i in read:
        print(i)

and I have got something like that:

['[0', ' 0', ' 1', ' 0]']

and I need:

[0, 0, 1, 0]

Any advises please?

pd13
  • 13
  • 1
  • 5
  • Use int() to cast to integer. Use replace() to remove the '[]' – Wimanicesir May 09 '21 at 18:52
  • Seems like your file is not a CSV file. Can you share sample content it has? – trincot May 09 '21 at 18:54
  • This is an [XY Problem](https://xyproblem.info/). That is not a CSV file. The file had Python lists written to it as strings. Fix the file to write a true CSV, or use [ast.literal_eval](https://docs.python.org/3/library/ast.html#ast.literal_eval) to convert the string back into a Python list. – Mark Tolonen May 09 '21 at 19:00
  • Thanks for help! But I already solve my problem, taking into consideration answers below. – pd13 May 09 '21 at 19:11
  • Still, they would not be the optimal answers if you would explain more about your context. Is the file really a CSV file? Or is it JSON? Or still something else? – trincot May 09 '21 at 19:12
  • It was txt file, `with open("statistics_test.txt") as file: read = csv.reader(file) for stat in read: statistics = [int(stat[0][1:]), int(stat[1]), int(stat[2]), int(stat[3][:2])]` It's working like I want – pd13 May 09 '21 at 19:15

3 Answers3

0
ad = ['[0', ' 0', ' 1', ' 0]']
for i in range(len(ad)):
    ad[i] = int(ad[i].replace("[", "").replace("]", ""))
print ad
[0, 0, 1, 0]
Raguram Gopi
  • 202
  • 1
  • 2
  • 8
  • Thanks for help! But I already solve my problem, taking into consideration all answers. @Ragu Ram – pd13 May 09 '21 at 19:12
0

Normally, the easiest solution would be a list comprehension where new = [int(a) for a in old]. However, in your case, the first and last elements of your list actually have brackets inside of them too.

Instead, you need to do something like:

new = [int("".join(filter(str.isdigit, a))) for a in old]

This is a pretty big one liner so lets break it down.

  1. The list comprehension iterates through each element in your list (I called it old) and names it a
  2. A is passed into the filter command with the function str.isdigit. This basically removes any character that isn't a digit. The issue with this, is that it returns an iterator and not a simple value.
  3. To fix the iterator problem, I wrapped the filter command with a "".join() command to convert it to a simple string value. This string will only have the number.
  4. Finally, we can wrap that entire thing with the int command which will transform your value into an int.

If you don't like the one-liner, it can also be done this way:

new = []
for a in old:
    filtered = filter(str.isdigit, a)
    num_str = "".join(filtered)
    num.append(int(num_str))

It's the same thing but a bit more verbose.

  • Thanks for help! But I already solve my problem, taking into consideration all answers. @Akilan Manivannan – pd13 May 09 '21 at 19:12
0
def parseToInt(s): 
    ...:     return int((s.replace('[', '')).replace(']', ''))
list(map(lambda x: parseToINt(x), a))
Alex
  • 236
  • 3
  • 11
  • Thanks for help! But I already solve my problem, taking into consideration all answers. @Alex – pd13 May 09 '21 at 19:11