write to file \t creates spaces not tabs [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So, I have a list of lists and trying to write the values to a file with tab delimited;
sorted_results=[
["test1", 01],
["test2", 02],
]
with open('outfile.txt', 'a') as write_file:
for i in sorted_results:
write_file.write("{}\t{}\n".format(i[0], i[1]))
The end result comes out as:
test1 01
test2 02
Values are space delimited not tab. What am I missing? If I add a space before \t then end result will have a space and a tab between the values.

You can read the file back in and inspect the resulting data.
>>> open('outfile.txt').read()
'test1\t1\ntest2\t2\n'
This shows that the tab character is indeed written to the file. If you are still in doubt use a hex editor to view the characters.

Related

python: csv.writer - commas between numbers in the output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
i suspect that this is a simple answer, but i cannot figure out the answer. (I did give it due diligence)
i wrote a simple python program to identify prime numbers. the program is function, but i'm receiving strange results in the output. when i have it write a number with multiple digits, each number is comma separated; for example, 13 is added to the document as 1,3. I would like to have a comma after each full number (13,) and don't want commas within the number (1,3 or 1,301). eventually, i want to have each number on its own row (one of the issue that i ran into in my g1 program is that the row became too long around 50mill ;-)
Any thoughts?
#!/bin/python3
import time
import os
import csv
folderLocation = "c:/notNow/"
primeName = "primeNumbers.csv"
# notPrimeName = "noPrimeNumbers.csv"
primePath=folderLocation + primeName
# notPrimePath=folderLocation + notPrimeName
no=13
os.makedirs(folderLocation)
f = open(primePath, "w")
writer = csv.writer(f)
writer.writerow(str(no))
output: 1,3
writerow expects a sequence of items (e.g list). A string is just seen as a sequence of individual characters, try this instead:
writer.writerow([no])

replace a char in string within triple ticks in python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have the following code :
Integration_Minute = 00
Integration_Schedule = '''{"cluster":{"scheduling":{"shutdownHours":{"isEnabled":true,"timeWindows":["Sat:00:00-Mon:04:xx","Tue:00:00-Tue:04:xx","Wed:00:00-Wed:04:xx","Thu:00:00-Thu:04:xx","Fri:00:00-Fri:04:xx"]}}}}'''
print(Integration_Schedule.replace("xx", Integration_Minute))
I want to replace xx with 00 but this doesnt work. Whats the best way of doing this in python ?
The error I get is :
File "s.py", line 4, in <module>
print(Integration_Schedule.replace("xx", Integration_Minute))
TypeError: expected a character buffer object
Integration_Minute = '00'
>>> print(Integration_Schedule.replace("xx", Integration_Minute))
{"cluster":{"scheduling":{"shutdownHours":{"isEnabled":true,"timeWindows":["Sat:
00:00-Mon:04:00","Tue:00:00-Tue:04:00","Wed:00:00-Wed:04:00","Thu:00:00-Thu:04:0
0","Fri:00:00-Fri:04:00"]}}}}

SyntaxError on filename when trying to read file? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to use machine learning to predict students' future grade, but am stuck at the beginning.
Seems like pandas.read_csv cannot read psv file, I got:
df = pd.read_csv(‘training.psv’)
^
SyntaxError: invalid character in identifier
How can I read psv file through python?
The problem is in the quotes symbol. You should either use ' or ".
Try:
df = pd.read_csv('training.psv')
pd.read_csv("training.psv", sep = "|", header = FALSE, stringsAsFactors = FALSE)
That should work

Index into a string in a list [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Suppose I have a list containing 9 fields, and the 9th field is a string.
Then print(line) will print the entire line, print(line[9]) will return something like:
1/0:.:PASS:90:204,90:201,88:48,39:-204,0,-90:-48,0,-39:14:9,5:5
but print(line[9[0:1]])
will return
File "FileParser.py", line 9, in ?
print(line[9[0:1]])
TypeError: unsubscriptable object
If I assign line[9] to a second var, then I can manipulate it like a string, but this seems like a silly extra step.
Is there a way to index directly into the string while still part of the list?
Thank you
You can index it like this
line[9][0:1]
line[9] will get the actual string and you get the range of characters from that string.
When you do line[9[0:1]] you are trying to get the range of values from the number 9, which is not possible. That is why your code fails.
Since line[9] is a string, then you have to do
line[9][0:1]
Let's say line[9] = "some string". Then you can manipulate it with
line[9][0:1]
which will be equivalent to
"some string"[0:1]

unindent does not match any outer indentation level [duplicate_same] [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
When i make error?
Don't see errors. Please, help me.
datafile = file('c:\\Users\\username\\Desktop\\test.txt')
for line in datafile:
if '5256' in line:
GLOBACCESS[jid]=100
reply('private', source, u'You license is valid!')
else:
reply('private', source, u'Incorrect password/jid')
Lines 2 and 3 are using spaces while everything else is using tabs. You always need to use the same, and should choose spaces per pep 8
Line 2 shouldn't be indented at all, with spaces or with tabs
for loop shouldn't be indented.
datafile = file('c:\\Users\\username\\Desktop\\test.txt')
for line in datafile:
if '5256' in line:
GLOBACCESS[jid]=100
reply('private', source, u'You license is valid!')
else:
reply('private', source, u'Incorrect password/jid')

Categories