Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading from a file
#11
(Jul-15-2020, 12:51 PM)GOTO10 Wrote: You aren't doing anything wrong, you just have more work to do. The Â\xa0 is a control character for a space. You could probably get rid of all of them by using the replace() method on each string, or you may need to look into using the unicodedata module or something similar. Also, if you want the numeric values to have a type other than string, you'll need to convert them to int or float as needed.
i dont understand how to use replace() in this case can you demonstrate
Reply
#12
You'll need to figure out how to get into your nested lists on your own, since this is the homework forum after all; but at the simplest level, what I'm suggesting would look like this:
some_string = 'Â\xa0Text_To_Keep'

# Using string.replace() to replace the 'Â\xa0' with an empty string ('').
new_string = some_string.replace('Â\xa0', '')

print(new_string)
Output:
Text_To_Keep
Reply
#13
Also, better form not to print from within the function. Rather, print the returned value in the calling script
print(get_csv_as_table(userFileName, userDelimiter))
Or, if you want to use the value and do something more with it
my_output = get_csv_as_table(userFileName, userDelimiter)
print(my_output)
Another nit pick - in naming your variables, it is considered proper form in Python to avoid camelCase and instead use underscores between_words_in_your variable names. Note how GOTO10 did that in his post above.
Reply
#14
(Jul-15-2020, 02:40 PM)GOTO10 Wrote: You'll need to figure out how to get into your nested lists on your own, since this is the homework forum after all; but at the simplest level, what I'm suggesting would look like this:
some_string = 'Â\xa0Text_To_Keep'

# Using string.replace() to replace the 'Â\xa0' with an empty string ('').
new_string = some_string.replace('Â\xa0', '')

print(new_string)
Output:
Text_To_Keep
i was told that the way im doing is incorrect because in my list i get all the values as a string how am i to approach this to convert the things after the delimiter \t to numbers.and can you be kind enough to tell where i have went wrong
Reply
#15
Have you modified your code based on any of the recent suggestions? What does it look like right now? I bet you already know a Python function that you can use to convert a string to an integer, so you need to find a way to apply a function like that to the string values in your lists that need to be converted.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Excel File reading vanjoe198 1 2,039 Mar-31-2021, 11:53 AM
Last Post: snippsat
  Weird problem with reading from file and performing calculations pineapple999 1 3,005 Jul-25-2019, 01:30 AM
Last Post: ichabod801
  Handling IO Error / Reading from file Expel 10 4,848 Jul-18-2019, 01:21 PM
Last Post: snippsat
  Reading an Unconventional CSV file OzSbk 2 3,877 May-17-2019, 12:15 PM
Last Post: MvGulik
  reading text file and writing to an output file precedded by line numbers kannan 7 10,409 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Reading of structured .mat (matlab) file sumit 2 3,420 May-24-2018, 12:12 PM
Last Post: sumit
  File Reading toxicxarrow 9 5,184 May-07-2018, 04:12 PM
Last Post: toxicxarrow
  reading all lines from a text file seadoofanatic 2 2,926 Mar-13-2018, 06:05 PM
Last Post: Narsimhachary
  Reading a text file fivestar 7 5,591 Oct-13-2017, 07:25 AM
Last Post: gruntfutuk
  Dictionary + File Reading palmtrees 2 4,875 Nov-15-2016, 05:16 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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