Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert list to interger
#1
Greetings,
The csv returns: [114436]
I want to get the values as an integer and not list, any pointers appreciated.
Thanks
Clive

##### CSV
id = open(manhole, encoding='utf-8' )
manhole_id = csv.reader(id)
next(id, None)

rows = ()
for row in manhole_id:
    data_manhole_id = [int(item) for item in row]
    print(data_manhole_id)
I still get:
Output:
[114436] [114474] [114514]
Not
Output:
114436 114474 114514
I also get an error: TypeError
TypeError: list indices must be integers or slices, not list
Reply
#2
I assume your file looks like this:
114436
114474
114514

This is not csv format. You do not need a CSV reader.
with open("data.csv", "r") as file:
    for line in file:
        print(int(line))
What you wrote is for data that is actually in csv format
114436,114474,114514
import csv

with open('data.csv', "r") as file:
    reader = csv.reader(file)
    for row in reader:
        data = [int(item) for item in row]
    print(data)
Output:
[114436, 114474, 114514]
Reply
#3
Hi Dean,
Thanks for the advice.

I changed the code:
        line = ()
        with open(data,  encoding='utf-8') as manhole_id:
            #csv_reader = reader(manhole_id)
            for line in manhole_id:
                next(manhole_id, None)
The next(manhole_id, None) is not working??
It is returning an extra line??

Thanks
Clive
Reply
#4
If the file looks like this:
Whatever
114436
114474
114514

You can skip the first line in the file.
with open("data.csv", "r") as file:
    next(file)
    for line in file:
        print(int(line))
Output:
114436 114474 114514
When using a contact manager (with), the file only remains open for code that is in the context block (indented 1 level deeper than the with). In your latest example the manhole_id file is closed before the for loop executes.
Reply
#5
Apologies typo...

It returns:
114806

114810

114831

114841

114843

NOT:
114806
114810
114831
114841
114843
Reply
#6
You are doing something wrong. My guess is you are printing the str which has a trailing linefeed. I thought you wanted to read the file and convert to int.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert string to float in list jacklee26 6 1,897 Feb-13-2023, 01:14 AM
Last Post: jacklee26
Thumbs Up Convert an Interger into any base !? [Solved] SpongeB0B 8 1,415 Jan-16-2023, 10:24 AM
Last Post: SpongeB0B
  convert a list to links Pir8Radio 3 1,095 Nov-28-2022, 01:52 PM
Last Post: Pir8Radio
  convert this List Comprehensions to loop jacklee26 8 1,503 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  Convert each element of a list to a string for processing tester_V 6 5,304 Jun-16-2021, 02:11 AM
Last Post: tester_V
  convert numbers into list lokesh 1 2,377 Jun-03-2021, 06:37 AM
Last Post: menator01
Question convert unlabeled list of tuples to json (string) masterAndreas 4 7,458 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
Star Convert Bytearray into List using list() Shlok 2 4,121 Feb-18-2021, 10:44 AM
Last Post: deanhystad
  convert List with dictionaries to a single dictionary iamaghost 3 2,851 Jan-22-2021, 03:56 PM
Last Post: iamaghost
Sad Convert python list to dictionary akanowhere 6 3,436 Dec-27-2020, 09:26 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020